1. Graphcs an Ulam Spiral, with start in the center,
iterating in a outward spiraling, counterclockwise fashion returns a list of
ulamGraph start_: 1, end_: 1600,
algorithm_: "standard", attempts_: 10, returnData_: False :
s start;
ulamPrimePoints ;
i 0; i allows us to properly iterate in a positive or negative direction on the x,
y axis of the Ulam spiral.
p 0, 0 ;
numberOfPrimes 0;
The graphing pattern of the Ulam Spiral oscillates between y and x,
1 and 1 directions. We capture this behavior with 2 seperate for loops
While s end,
Here we iterate through in a positive or negative x direction
Do
If isPrimeQ s, algorithm, attempts ,
numberOfPrimes ; ulamPrimePoints Append ulamPrimePoints, p ;
p p 1 ^i 1, 0 ;
s ;,
x, 0, i ;
Here we iterate through in a positive or negative y direction
Do
If isPrimeQ s, algorithm, attempts ,
numberOfPrimes ; ulamPrimePoints Append ulamPrimePoints, p ;
p p 1 ^i 0, 1 ;
s ;,
y, 0, i ;
i ;
;
Sometimes, we may not want to print, and only want the ulam plot information
If returnData, Return ulamPrimePoints ,
Print "The number of primes in the range ",
start, " to ", end, " is ", numberOfPrimes ;
Print "The average probability of finding a prime in this range is ",
1.0 numberOfPrimes end start ;
range Ceiling Ceiling end start 2 ;
coordinateRange range, range ;
ListPlot ulamPrimePoints,
PlotRange coordinateRange , coordinateRange , AspectRatio 1
2. This function acquires the correct primality algorithm to test some integer n
isPrimeQ number_, algorithm_, attempts_ :
If MatchQ algorithm , "standard" , Return PrimeQ number ;
If MatchQ algorithm , "fermat" , Return fermatPrimeQ number, attempts ;
If MatchQ algorithm , "naive" , Return naivePrimeQ number ;
This is the most basic deterministic primality testing algorithm,
testing each divisor below the number in question until that numbers square root
naivePrimeQ number_ :
q 2;
While q number ,
If Mod number, q 0, Return False , q
;
Return True ;
This is Fermat's probabilistic algorithm, utilizing Fermat's little theorem
fermatPrimeQ n_, attempts_ :
i 0;
While i attempts,
a Random Integer, 1, n 1 ;
If expMod a, n, n a, Return False , i
;
Return True ;
This is a recursive modulus function that takes in an explicit
base, exponent, utilizing the property of exponents a^2p a^p ^2
expMod base_, exp_, m_ :
If exp 0, 1,
If Mod exp, 2 0, Mod expMod base, exp 2, m 2
, m ,
Mod base expMod base, exp 1 , m , m
ulamGraph 1, 1600, "standard"
ulamGraph 1, 1600, "naive"
ulamGraph 1, 20 000
2 UlamFinal.nb
5. 20 10 10 20
20
10
10
20
The number of primes in the range 1 to 1600 is 272
The average probability of finding a prime in this range is 0.170106
UlamFinal.nb 5
6. 20 10 10 20
20
10
10
20
The number of primes in the range 1 to 20 000 is 2264
The average probability of finding a prime in this range is 0.113206
6 UlamFinal.nb
7. 60 40 20 20 40 60
60
40
20
20
40
60
The number of primes in the range 20 000 to 40 000 is 1943
The average probability of finding a prime in this range is 0.09715
UlamFinal.nb 7
8. 60 40 20 20 40 60
60
40
20
20
40
60
The number of primes in the range 100 000 000 000 000 to 100 000 000 010 000 is 305
The average probability of finding a prime in this range is 0.0305
8 UlamFinal.nb
9. 40 20 20 40
40
20
20
40
The number of primes in the range 100 000 000 010 000 to 100 000 000 050 000 is 1179
The average probability of finding a prime in this range is 0.029475
UlamFinal.nb 9
10. 100 50 50 100
100
50
50
100
The number of primes in the range 10 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
to 10 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 010 000 is 80
The average probability of finding a prime in this range is 0.008
10 UlamFinal.nb
11. 40 20 20 40
40
20
20
40
The number of primes in the range 10 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
to 10 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 100 000 is 791
The average probability of finding a prime in this range is 0.00791
UlamFinal.nb 11
12. 150 100 50 50 100 150
150
100
50
50
100
150
The number of primes in the range
100 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
000 000 000 000 000 000 000 000 to
100 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
000 000 000 000 000 000 010 000 is 34
The average probability of finding a prime in this range is 0.0034
12 UlamFinal.nb
13. 40 20 20 40
40
20
20
40
The number of primes in the range
100 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
000 000 000 000 000 000 000 000 to
100 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000
000 000 000 000 000 000 100 000 is 434
The average probability of finding a prime in this range is 0.00434
UlamFinal.nb 13
16. 40 20 20 40
40
20
20
40
9.922618282587337`*^-16 , True Built in prime test for 32416187567
1.1229999999999967` , True Naive prime test for 32416187567
6.543376951384516`*^-15 , True Fermat prime test with 10 attempts, for 32416187567
0.4520000000000069` , True Fermat prime test with 1,000 attempts, for 32416187567
4.289999999999993` , True Fermat prime test with 10,000 attempts, for 32416187567
0.016000000000005718` , Null
Built in prime test for all the numbers between 1000000 and 1010000
6.598999999999997` , Null
Naive prime test for all the numbers between 1000000 and 1010000
3.9310000000000063` , Null Fermat prime test with 10 attempts,
for all the numbers between 1000000 and 1010000
16 UlamFinal.nb
17. 19.921000000000006` , Null Fermat prime test with 100 attempts,
for all the numbers between 1000000 and 1010000
0.015999999999988926` , Null
Built in prime test for all the numbers between 100000000 and 100010000
4.8520000000000065` , Null Fermat prime test with 10 attempts,
for all the numbers between 100000000 and 100010000
20.716999999999988` , Null Fermat prime test with 100 attempts,
for all the numbers between 100000000 and 100010000
UlamFinal.nb 17