#Written by Dr. Paul Raff, Rutgers University, Jan. 28, 2010 #For Dr. Z.'s Math 640 contest HelpPR:=proc(): print(`PrimeListPR(N)`): end: # Finds the next prime, trying the number given. # Assumes the list L is all the primes less than input n NextPrime:=proc(L,n) local i,len,sqrtn; len:=nops(L); sqrtn:=floor(sqrt(n)); for i from 1 to len while L[i]<=sqrtn do if n mod L[i] = 0 then return NextPrime(L,n+1); fi: od: # At this point, it's a prime! return n; end: PrimeListPR:=proc(N) local L,i,curprime,oldprime,temp; if N=0 then return []; elif N=1 then return [2]; elif N=2 then return [2,3]; elif N=3 then return [2,3,5]; elif N=4 then return [2,3,5,7]; else L:=[2,3,5,7]; oldprime:=5; curprime:=7; for i from 5 to N do temp:=curprime; if (curprime-oldprime = 2) then curprime:=NextPrime(L,temp+4); else curprime:=NextPrime(L,temp+2); fi: L:=[op(L),curprime]; oldprime:=temp; od: fi: return L: end: