Can I return a list in Mathematica functions?
In my code, I am trying to return a list of numbers from my function, but it just gives me null .
sifra[zprava_, klic_] := Module[
{c, n, e, m, i, z, pocCyklu, slovo},
pocCyklu = Ceiling[Divide[StringLength[zprava], 5]];
c = Array[{}, pocCyklu];
z = Partition[Characters[zprava], 5, 5, 1, {}];
For[i = 1, i < pocCyklu + 1, i++,
slovo = StringJoin @ z[[i]];
m = StringToInteger[slovo];
n = klic[[1]];
e = klic[[2]];
c[[i]] = PowerMod[m, e, n];
]
Return[c]
];
sif = sifra[m, verejny]
After completing the cycles, there c
should be 2 numbers.
Print[c]
works OK, it prints a list with two elements in it, but sif
null . Return[c]
gives me:
Null Return [{28589400926821874625642026431141504822, 2219822858062194181357669868096}]
+3
source to share
1 answer
You can write a function like this:
sifra[zprava_, klic_] := Module[{c, n, e, m, i, z, pocCyklu, slovo},
pocCyklu = Ceiling[Divide[StringLength[zprava], 5]];
c = ConstantArray[{}, pocCyklu];
z = Partition[Characters[zprava], 5, 5, 1, {}];
For[i = 1, i < pocCyklu + 1, i++,
slovo = StringJoin@z[[i]];
m = ToExpression[slovo];
{n, e} = klic;
c[[i]] = PowerMod[m, e, n]];
c]
Demonstration of using the example data:
sifra["9385637605763057836503784603456", {124, 2}]
{20, 97, 41, 9, 4, 113, 36}
You can also write a function like this:
sifra[zprava_, {n_, e_}] := Module[{z},
z = Partition[Characters[zprava], 5, 5, 1, {}];
Map[PowerMod[ToExpression[StringJoin[#]], e, n] &, z]]
+1
source to share