# How to use every number between 2^50 and 2^100 without having to rewrite the code? [closed]

What I have currently written returns a numerical value and stores it in b100. p and q are random integers of size 2^50. I want to use the same code to calculate values where p and q ranges from 2^50 - 2^100. How do I do this without having to repeat the code below but and changing p and q 50 times, i.e. p=2^51 and q=2^51 run the code, p=2^52 and q=2^52 run the code etc.

This is my current code:

ClearAll["*"]

toString[n_, B_] := Module[
{ascii = {}, q = n},
While[
q != 0,
AppendTo[ascii, Mod[q, B]];
q = Quotient[q, B]
];
Return[ascii]
]

RSAcrack [cipher_, n_, e_] := Module[
{p, q, \[Phi], d, m, decryptMessage},
{p, q} = Map[Function[x, x[[1]]], FactorInteger[n]];
\[Phi] = (p - 1)*(q - 1);
d = PowerMod[e, -1, \[Phi]];
m = Map[Function[x, PowerMod[x, d, n]], cipher];
decryptMessage =
Map[Function[x, FromCharacterCode[toString[x, 256]]], m];
Return[StringJoin[decryptMessage]];
]

p = NextPrime[RandomInteger[{2^50}]];

q = NextPrime[RandomInteger[{2^50}]];

n = p*q;

\[Phi] = (p - 1)*(q - 1);

rnd := RandomInteger[{10^3, 10^4}]

While[GCD[e = rnd, \[Phi]] != 1];

d = PowerMod[e, -1, \[Phi]];

message = "Simplicity is a great virtue but it requires hard work to achieve \
it and education to appreciate it. And to make matters worse: complexity \
sells better.Edsger Dijkstra";

messageAscii = Partition[ToCharacterCode[message], UpTo[12]];
B = 256;

M = MapIndexed[#1 . B^(Range[Length[messageAscii[[First[#2]]]]] - 1) &,
messageAscii];
c = PowerMod[M, e, n];

{b100, v} = AbsoluteTiming[RSAcrack[c, n, e]]
b100

• Most of the code you posted seems quite irrelevant for your question (and so is the title of your question). AbsoluteTiming returns a list {time, result}. Therefore, store both values: {t, res} = AbsoluteTiming[RSAcrack[c, n, e]];, then add the time to your list of times: AppendTo[time, t];. Commented Oct 12, 2021 at 22:34
• Use SeedRandom to make random numbers repeatable. This may be helpful, if you want to reproduce the results. Commented Oct 12, 2021 at 22:49
• Advice for future questions: If you start a post with four pages of code and no text, it is likely that people will pass on your question as soon as they see it. Ask smart: walk the reader through your problem, from beginning to end (and not the reverse). Make it easy to understand your question. Include all that is necessary and exclude everything else (are 4 pages of code necessary?) Commented Oct 13, 2021 at 7:21

Without solving your homework, I will try to help you resolve the problem you have at the moment.

Let's say the following command is executed. It gives you the time required for calculations and the results.

AbsoluteTiming[FactorInteger[2^200 - 1]]

{0.0296725, {{3, 1}, {5, 3}, {11, 1}, {17, 1}, {31, 1}, {41, 1}, {101,
1}, {251, 1}, {401, 1}, {601, 1}, {1801, 1}, {4051, 1}, {8101,
1}, {61681, 1}, {268501, 1}, {340801, 1}, {2787601,
1}, {3173389601, 1}}}


As a means of storing these values, you need two variables:

{t, v} = AbsoluteTiming[FactorInteger[2^200 - 1]]


-----------------

You are (more) interested in the time part of the answer getting appended to a time array and you want to run the RSA function while changing a variable. Define empty "timearray" and "valarray" lists.

tarray = {};
valarray = {};
Table[{AppendTo[tarray, First@#], AppendTo[valarray, Last@#]} &@
AbsoluteTiming[FactorInteger[2^i - 1]], {i, 200, 210}];


Here the factors from i=200 to i=210 along with time required for calculation will be appended to the arrays. Looking at the timearray:

tarray

{0.0126698, 0.0183797, 0.046207, 0.00825064, 0.000598582, 0.552137, \
0.371533, 0.0427462, 0.0165321, 0.244372, 0.00647573}


and since I started at i=200, (you can append your own parameter)

t2 = Transpose[{199 + Range[Length@tarray], tarray}]

{{200, 0.0126698}, {201, 0.0183797}, {202, 0.046207}, {203,
0.00825064}, {204, 0.000598582}, {205, 0.552137}, {206,
0.371533}, {207, 0.0427462}, {208, 0.0165321}, {209,
0.244372}, {210, 0.00647573}}


This last step could have been achieved inside the Table but this is a demo.

ListLogPlot[t2, Joined -> True]


The fitting part, I will leave for you as a further exercise.