Some procedures for testing for good primes.

In this section I will present a few simple procedures. They are written for clarity, not for typing efficiency. I am sure that you could write a more compact version of each of the procedures.

We will

In the RSA cryptosystem, it is useful to have a prime number p such that (p-1)/2 has large prime factors. For example if (p-1)/2 is itself prime that would be ideal. Let us write a simple test for a prime to see if it satisfies this condition.

> goodprime := proc(p)
local pp, bool;

pp := (p-1)/2;
if isprime(pp)
then bool := true;
else bool := false;
fi;
RETURN(bool);
end proc;

goodprime := proc (p) local pp, bool; pp := 1/2*p-1...
goodprime := proc (p) local pp, bool; pp := 1/2*p-1...
goodprime := proc (p) local pp, bool; pp := 1/2*p-1...
goodprime := proc (p) local pp, bool; pp := 1/2*p-1...

There are a few things to notice in this simple example.

1) The if statement must be terminated with either "end if" or its abbreviation "fi".

2) The else statement is optional. You can also have a sequence of else if statements, written "elif".

3) If you hold down the <shift> key when typing <return> you don't get a new cursor and the annoying warning message.

4) I like to explicitly say what the procedure should return, using RETURN() as shown.

I'm not completely clear on the rules if RETURN is not specified.

Now let's make a list of all good primes between m and n.

> goodprimelist := proc(m,n)
local i, plist;

plist := [];
for i from m to n do
if (isprime(i) and goodprime(i))
then plist := [op(plist), i];
fi;
od;
RETURN(plist);
end proc;

goodprimelist := proc (m, n) local i, plist; plist ...
goodprimelist := proc (m, n) local i, plist; plist ...
goodprimelist := proc (m, n) local i, plist; plist ...
goodprimelist := proc (m, n) local i, plist; plist ...
goodprimelist := proc (m, n) local i, plist; plist ...
goodprimelist := proc (m, n) local i, plist; plist ...

Notes: 1) We closed both the if and the do statements. 2) The indentation is my choice for legibility. 3) I had to initialize plist . 4) Extending a list is done via the op() command.

> goodprimelist(100,1000);

[107, 167, 179, 227, 263, 347, 359, 383, 467, 479, ...

Now let's count how many good primes there are less than 2^(i+1) and

graph the result. This indicate tell how easy it is to find such primes, and thus the feasibility of finding them for an RSA cryptosystem.

For comparison, the Prine Number Theorem says that the number of primes less than 2^i is approximately ((2^i / i * log(2) ).

>

> numgoodprimes := proc(pow)
local i, tot_cnt, new_cnt, countlist;

countlist := [];
tot_cnt := 0;
for i from 2 to pow do
new_cnt := nops(goodprimelist(2^i, 2^(i+1)));
tot_cnt := tot_cnt + new_cnt;
countlist := [op(countlist), [i,tot_cnt]];
od;
RETURN(countlist);
end proc;

numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...
numgoodprimes := proc (pow) local i, tot_cnt, new_c...

> datalist := numgoodprimes(15);

datalist := [[2, 2], [3, 3], [4, 4], [5, 6], [6, 8]...
datalist := [[2, 2], [3, 3], [4, 4], [5, 6], [6, 8]...

Now go back to the numgoodprimes function and change it so that it prints out the number of goodprimes in each range 2^i .... 2^(i+1), so that we can keep track of how long it takes to do range (it should increase exponentially). Use

print("i is " i);

print( "the number of goodprimes in the range" (2^i, 2^(i+1)) );
print( "is " (new_+cnt));

> with(plots);

Warning, the name changecoords has been redefined

[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...
[animate, animate3d, animatecurve, changecoords, co...

Now we will plot the datalist with the number of good primes and compare it to what we might expect according to the prime number theorem. There number of primes less than t is roughly t/ln(t). We might expect that t and (t-1)/2 would both be prime for t/ (ln(t)*ln(t/2)) values of t.

> plot1 := plot(datalist, color = red);

plot1 := INTERFACE_PLOT(CURVES([[2., 2.], [3., 3.],...
plot1 := INTERFACE_PLOT(CURVES([[2., 2.], [3., 3.],...
plot1 := INTERFACE_PLOT(CURVES([[2., 2.], [3., 3.],...

> plot2 := plot(2^t/(log(2^t)*log(2^(t-1))), t=2..15, color = blue):

> display(plot1,plot2);

[Maple Plot]