Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 63cf41e2a14aadbd54362b40e22f657602ff19a8 @stevekochscience committed May 3, 2012
Showing with 3,933,540 additions and 0 deletions.
  1. BIN 30M swiss prime loneliness.png
  2. +25 −0 README
  3. +51 −0 calclonelyfromprimes2.py
  4. +28 −0 graphlonely2.r
  5. +430 −0 ls3000.csv
  6. +1,857,859 −0 ls30000000.csv
  7. +430 −0 p3000.txt
  8. +216,816 −0 p3000000.txt
  9. +1,857,859 −0 p30000000.txt
  10. +42 −0 sieve.py
BIN 30M swiss prime loneliness.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 README
@@ -0,0 +1,25 @@
+This repository holds some R and python code as well as some csv and text files with lists of prime numbers
+
+Code is public domain, by Steven J. Koch, stevekochscience@gmail.com. Except possibly the "sieve.py" code, which I adapted
+from Phillip Dorrell on github "zero-comments". I didn't see his license information there, and it's a small bit of code,
+so I'm assuming it's also public domain.
+
+Instructions:
+
+1. (optional) Edit file "sieve.py" and set the SIZE parameter to the upper limit of primes that you want to generate.
+ Some files already exist if you don't want to re-run sieve.py
+
+2. (optional) Run "python sieve.py". This will create a file of type p###.txt which lists all the prime numbers up to
+ value SIZE
+
+3. Edit file "calclonelyfromprimes2.py" and set the SIZE parameter to correspond to a p###.txt file that you want to process
+
+4. Run "python calclonelyfromprimes2.py". If a correct p###.txt file is loaded, it will calculate the loneliness and set the
+ primes flag. It will output a csv file with name "ls###.csv" and has three columns: integer prime, float loneliness, integer
+ Swiss prime flag
+
+5. Edit the R file, graphlonely2.r and change the filename to the corresponding ls###.csv file that you want to display
+
+6. Run the R file to display the graph. Magenta circles are the swiss primes. Black circles are loneliness of all the primes.
+ If you want to save the graph, un-comment the png("filename.png") line and the last line, dev.off(). (I don't really
+ understand this, but it works)
51 calclonelyfromprimes2.py
@@ -0,0 +1,51 @@
+# this file reads a prime number file (each number in own row) and calculates loneliness
+
+import math
+
+SIZE = 30000000
+
+def loneliness(mylist, i):
+ """If the number is at the edge of the list, swiss flag is set to -1. Otherwise, 1 means true and 0 means false.
+ The lness is the loneliness which is geometric mean of distance between neighboring primes. If these distances
+ are equal it will be "1" for true as swiss. If lness is an integer, it means that the two distances combined have an
+ an even number of each divisor. For example, distances = 2 and 50."""
+ # print i, mylist[i]
+ if i == 0:
+ lness = mylist[i+1]-mylist[i]
+ swiss = -1
+ elif i == (len(mylist) - 1):
+ lness = mylist[i] - mylist[i-1]
+ swiss = -1
+ else:
+ assert i>0 and i < len(mylist) - 1
+ lness = math.sqrt( (mylist[i+1] - mylist[i]) * (mylist[i] - mylist[i-1]) )
+ if mylist[i+1] - mylist[i] == mylist[i] - mylist[i-1]:
+ swiss = 1
+ else:
+ swiss = 0
+ return [lness, swiss]
+
+primes=[]
+
+fh = open("p%s.txt" %SIZE)
+
+for line in fh:
+ primes.append(int(line))
+
+fh.close()
+
+lonely = []
+swiss = []
+
+for j in range(0,len(primes)):
+ result = loneliness(primes, j)
+ lonely.append(result[0])
+ swiss.append(result[1])
+
+outfile = open ('ls%s.csv' %SIZE, 'w')
+
+for j in range(0,len(primes)):
+ outfile.write("%d,%f,%d\n" % (primes[j], lonely[j], swiss[j]))
+
+outfile.close()
+
28 graphlonely2.r
@@ -0,0 +1,28 @@
+# Code is public domain--Steven J. Koch
+
+# this R code requires a special csv file listing primes, loneliness, and swiss prime flag.
+# I create this file with python code, "calclonelyfromprimes2.py"
+# read in the prime numbers. first column is prime number, second column is loneliness, third column is a flag for whether it is a swiss
+# prime. 1 = true, 0 = false, and -1 means there was only one neighbor.
+lonely <- read.table("ls30000000.csv", sep=",", header=FALSE)
+names(lonely) <- c("prime", "lness", "swiss")
+
+# find subset of "Swiss primes." I.e. those primes that are equidistant between the two neighboring prime numbers
+swiss <- subset(lonely, swiss == 1)
+
+
+#save the graph to a PNG file
+#png("30M swiss prime loneliness.png")
+
+# create the plot
+plot(lonely$prime, lonely$lness, log="x", pch=1, cex=0.5, main="Prime loneliness", xlab="number", ylab="loneliness",
+ ylim = c(0, max(lonely$fit, lonely$lness)))
+points(swiss$prime, swiss$lness, pch=21, cex=1.1, col="magenta")
+
+legend("topleft", c("Loneliness, all primes", "Loneliness, Swiss primes"), pch = c(1, 21),
+ col = c("black", "magenta"), inset = 0.1)
+
+# this next command is necessary to make the graph save, I don't know why. It makes the graph disappear from the screen though
+# I also have trouble viewing the file with external editor unless I close out of R. Obviously doing something wrong.
+#dev.off()
+
430 ls3000.csv
@@ -0,0 +1,430 @@
+2,1.000000,-1
+3,1.414214,0
+5,2.000000,1
+7,2.828427,0
+11,2.828427,0
+13,2.828427,0
+17,2.828427,0
+19,2.828427,0
+23,4.898979,0
+29,3.464102,0
+31,3.464102,0
+37,4.898979,0
+41,2.828427,0
+43,2.828427,0
+47,4.898979,0
+53,6.000000,1
+59,3.464102,0
+61,3.464102,0
+67,4.898979,0
+71,2.828427,0
+73,3.464102,0
+79,4.898979,0
+83,4.898979,0
+89,6.928203,0
+97,5.656854,0
+101,2.828427,0
+103,2.828427,0
+107,2.828427,0
+109,2.828427,0
+113,7.483315,0
+127,7.483315,0
+131,4.898979,0
+137,3.464102,0
+139,4.472136,0
+149,4.472136,0
+151,3.464102,0
+157,6.000000,1
+163,4.898979,0
+167,4.898979,0
+173,6.000000,1
+179,3.464102,0
+181,4.472136,0
+191,4.472136,0
+193,2.828427,0
+197,2.828427,0
+199,4.898979,0
+211,12.000000,1
+223,6.928203,0
+227,2.828427,0
+229,2.828427,0
+233,4.898979,0
+239,3.464102,0
+241,4.472136,0
+251,7.745967,0
+257,6.000000,1
+263,6.000000,1
+269,3.464102,0
+271,3.464102,0
+277,4.898979,0
+281,2.828427,0
+283,4.472136,0
+293,11.832160,0
+307,7.483315,0
+311,2.828427,0
+313,2.828427,0
+317,7.483315,0
+331,9.165151,0
+337,7.745967,0
+347,4.472136,0
+349,2.828427,0
+353,4.898979,0
+359,6.928203,0
+367,6.928203,0
+373,6.000000,1
+379,4.898979,0
+383,4.898979,0
+389,6.928203,0
+397,5.656854,0
+401,5.656854,0
+409,8.944272,0
+419,4.472136,0
+421,4.472136,0
+431,4.472136,0
+433,3.464102,0
+439,4.898979,0
+443,4.898979,0
+449,6.928203,0
+457,5.656854,0
+461,2.828427,0
+463,2.828427,0
+467,6.928203,0
+479,9.797959,0
+487,5.656854,0
+491,5.656854,0
+499,5.656854,0
+503,4.898979,0
+509,8.485281,0
+521,4.898979,0
+523,6.000000,0
+541,10.392305,0
+547,7.745967,0
+557,7.745967,0
+563,6.000000,1
+569,3.464102,0
+571,3.464102,0
+577,7.745967,0
+587,7.745967,0
+593,6.000000,1
+599,3.464102,0
+601,3.464102,0
+607,6.000000,1
+613,4.898979,0
+617,2.828427,0
+619,4.898979,0
+631,10.954451,0
+641,4.472136,0
+643,2.828427,0
+647,4.898979,0
+653,6.000000,1
+659,3.464102,0
+661,4.898979,0
+673,6.928203,0
+677,4.898979,0
+683,6.928203,0
+691,8.944272,0
+701,8.944272,0
+709,8.944272,0
+719,8.944272,0
+727,6.928203,0
+733,6.000000,1
+739,4.898979,0
+743,5.656854,0
+751,6.928203,0
+757,4.898979,0
+761,5.656854,0
+769,5.656854,0
+773,7.483315,0
+787,11.832160,0
+797,10.954451,0
+809,4.898979,0
+811,4.472136,0
+821,4.472136,0
+823,2.828427,0
+827,2.828427,0
+829,4.472136,0
+839,11.832160,0
+853,7.483315,0
+857,2.828427,0
+859,2.828427,0
+863,7.483315,0
+877,7.483315,0
+881,2.828427,0
+883,2.828427,0
+887,8.944272,0
+907,8.944272,0
+911,5.656854,0
+919,8.944272,0
+929,8.944272,0
+937,5.656854,0
+941,4.898979,0
+947,6.000000,1
+953,9.165151,0
+967,7.483315,0
+971,4.898979,0
+977,6.000000,1
+983,6.928203,0
+991,6.928203,0
+997,8.485281,0
+1009,6.928203,0
+1013,4.898979,0
+1019,3.464102,0
+1021,4.472136,0
+1031,4.472136,0
+1033,3.464102,0
+1039,7.745967,0
+1049,4.472136,0
+1051,4.472136,0
+1061,4.472136,0
+1063,3.464102,0
+1069,10.392305,0
+1087,8.485281,0
+1091,2.828427,0
+1093,2.828427,0
+1097,4.898979,0
+1103,6.000000,1
+1109,6.928203,0
+1117,6.928203,0
+1123,6.000000,1
+1129,11.489125,0
+1151,6.633250,0
+1153,4.472136,0
+1163,8.944272,0
+1171,8.944272,0
+1181,7.745967,0
+1187,6.000000,1
+1193,6.928203,0
+1201,9.797959,0
+1213,6.928203,0
+1217,4.898979,0
+1223,6.000000,1
+1229,3.464102,0
+1231,3.464102,0
+1237,8.485281,0
+1249,10.954451,0
+1259,13.416408,0
+1277,6.000000,0
+1279,2.828427,0
+1283,4.898979,0
+1289,3.464102,0
+1291,3.464102,0
+1297,4.898979,0
+1301,2.828427,0
+1303,2.828427,0
+1307,6.928203,0
+1319,4.898979,0
+1321,3.464102,0
+1327,14.282857,0
+1361,14.282857,0
+1367,6.000000,1
+1373,6.928203,0
+1381,12.000000,0
+1399,13.416408,0
+1409,11.832160,0
+1423,7.483315,0
+1427,2.828427,0
+1429,2.828427,0
+1433,4.898979,0
+1439,6.928203,0
+1447,5.656854,0
+1451,2.828427,0
+1453,3.464102,0
+1459,8.485281,0
+1471,10.954451,0
+1481,4.472136,0
+1483,2.828427,0
+1487,2.828427,0
+1489,2.828427,0
+1493,4.898979,0
+1499,8.485281,0
+1511,12.000000,1
+1523,9.797959,0
+1531,9.797959,0
+1543,8.485281,0
+1549,4.898979,0
+1553,4.898979,0
+1559,6.928203,0
+1567,5.656854,0
+1571,5.656854,0
+1579,5.656854,0
+1583,7.483315,0
+1597,7.483315,0
+1601,4.898979,0
+1607,3.464102,0
+1609,2.828427,0
+1613,4.898979,0
+1619,3.464102,0
+1621,3.464102,0
+1627,7.745967,0
+1637,14.142136,0
+1657,10.954451,0
+1663,4.898979,0
+1667,2.828427,0
+1669,6.928203,0
+1693,9.797959,0
+1697,2.828427,0
+1699,4.472136,0
+1709,10.954451,0
+1721,4.898979,0
+1723,4.472136,0
+1733,8.944272,0
+1741,6.928203,0
+1747,6.000000,1
+1753,6.000000,1
+1759,10.392305,0
+1777,10.392305,0
+1783,4.898979,0
+1787,2.828427,0
+1789,4.898979,0
+1801,10.954451,0
+1811,10.954451,0
+1823,9.797959,0
+1831,11.313708,0
+1847,14.966630,0
+1861,9.165151,0
+1867,4.898979,0
+1871,2.828427,0
+1873,2.828427,0
+1877,2.828427,0
+1879,4.472136,0
+1889,10.954451,0
+1901,8.485281,0
+1907,6.000000,1
+1913,10.392305,0
+1931,6.000000,0
+1933,5.656854,0
+1949,5.656854,0
+1951,6.633250,0
+1973,11.489125,0
+1979,6.928203,0
+1987,6.928203,0
+1993,4.898979,0
+1997,2.828427,0
+1999,2.828427,0
+2003,5.656854,0
+2011,6.928203,0
+2017,7.745967,0
+2027,4.472136,0
+2029,4.472136,0
+2039,11.832160,0
+2053,11.832160,0
+2063,7.745967,0
+2069,8.485281,0
+2081,4.898979,0
+2083,2.828427,0
+2087,2.828427,0
+2089,4.472136,0
+2099,10.954451,0
+2111,4.898979,0
+2113,5.656854,0
+2129,5.656854,0
+2131,3.464102,0
+2137,4.898979,0
+2141,2.828427,0
+2143,4.472136,0
+2153,8.944272,0
+2161,12.000000,0
+2179,20.784610,0
+2203,9.797959,0
+2207,4.898979,0
+2213,6.928203,0
+2221,11.313708,0
+2237,5.656854,0
+2239,2.828427,0
+2243,5.656854,0
+2251,11.313708,0
+2267,5.656854,0
+2269,2.828427,0
+2273,5.656854,0
+2281,6.928203,0
+2287,6.000000,1
+2293,4.898979,0
+2297,6.928203,0
+2309,4.898979,0
+2311,6.633250,0
+2333,11.489125,0
+2339,3.464102,0
+2341,3.464102,0
+2347,4.898979,0
+2351,4.898979,0
+2357,9.165151,0
+2371,9.165151,0
+2377,4.898979,0
+2381,2.828427,0
+2383,3.464102,0
+2389,4.898979,0
+2393,4.898979,0
+2399,8.485281,0
+2411,8.485281,0
+2417,6.000000,1
+2423,9.165151,0
+2437,7.483315,0
+2441,4.898979,0
+2447,8.485281,0
+2459,9.797959,0
+2467,6.928203,0
+2473,4.898979,0
+2477,10.198039,0
+2503,21.633308,0
+2521,13.416408,0
+2531,8.944272,0
+2539,5.656854,0
+2543,4.898979,0
+2549,3.464102,0
+2551,3.464102,0
+2557,11.489125,0
+2579,16.248077,0
+2591,4.898979,0
+2593,5.656854,0
+2609,11.313708,0
+2617,5.656854,0
+2621,6.928203,0
+2633,12.961481,0
+2647,11.832160,0
+2657,4.472136,0
+2659,2.828427,0
+2663,5.656854,0
+2671,6.928203,0
+2677,6.000000,1
+2683,4.898979,0
+2687,2.828427,0
+2689,2.828427,0
+2693,4.898979,0
+2699,6.928203,0
+2707,5.656854,0
+2711,2.828427,0
+2713,3.464102,0
+2719,7.745967,0
+2729,4.472136,0
+2731,4.472136,0
+2741,8.944272,0
+2749,5.656854,0
+2753,7.483315,0
+2767,11.832160,0
+2777,10.954451,0
+2789,4.898979,0
+2791,3.464102,0
+2797,4.898979,0
+2801,2.828427,0
+2803,5.656854,0
+2819,14.966630,0
+2833,7.483315,0
+2837,4.898979,0
+2843,6.928203,0
+2851,6.928203,0
+2857,4.898979,0
+2861,8.485281,0
+2879,12.000000,0
+2887,8.944272,0
+2897,7.745967,0
+2903,6.000000,1
+2909,6.928203,0
+2917,8.944272,0
+2927,10.954451,0
+2939,12.961481,0
+2953,7.483315,0
+2957,4.898979,0
+2963,6.000000,1
+2969,3.464102,0
+2971,7.483315,0
+2999,28.000000,-1
1,857,859 ls30000000.csv
1,857,859 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
430 p3000.txt
@@ -0,0 +1,430 @@
+2
+3
+5
+7
+11
+13
+17
+19
+23
+29
+31
+37
+41
+43
+47
+53
+59
+61
+67
+71
+73
+79
+83
+89
+97
+101
+103
+107
+109
+113
+127
+131
+137
+139
+149
+151
+157
+163
+167
+173
+179
+181
+191
+193
+197
+199
+211
+223
+227
+229
+233
+239
+241
+251
+257
+263
+269
+271
+277
+281
+283
+293
+307
+311
+313
+317
+331
+337
+347
+349
+353
+359
+367
+373
+379
+383
+389
+397
+401
+409
+419
+421
+431
+433
+439
+443
+449
+457
+461
+463
+467
+479
+487
+491
+499
+503
+509
+521
+523
+541
+547
+557
+563
+569
+571
+577
+587
+593
+599
+601
+607
+613
+617
+619
+631
+641
+643
+647
+653
+659
+661
+673
+677
+683
+691
+701
+709
+719
+727
+733
+739
+743
+751
+757
+761
+769
+773
+787
+797
+809
+811
+821
+823
+827
+829
+839
+853
+857
+859
+863
+877
+881
+883
+887
+907
+911
+919
+929
+937
+941
+947
+953
+967
+971
+977
+983
+991
+997
+1009
+1013
+1019
+1021
+1031
+1033
+1039
+1049
+1051
+1061
+1063
+1069
+1087
+1091
+1093
+1097
+1103
+1109
+1117
+1123
+1129
+1151
+1153
+1163
+1171
+1181
+1187
+1193
+1201
+1213
+1217
+1223
+1229
+1231
+1237
+1249
+1259
+1277
+1279
+1283
+1289
+1291
+1297
+1301
+1303
+1307
+1319
+1321
+1327
+1361
+1367
+1373
+1381
+1399
+1409
+1423
+1427
+1429
+1433
+1439
+1447
+1451
+1453
+1459
+1471
+1481
+1483
+1487
+1489
+1493
+1499
+1511
+1523
+1531
+1543
+1549
+1553
+1559
+1567
+1571
+1579
+1583
+1597
+1601
+1607
+1609
+1613
+1619
+1621
+1627
+1637
+1657
+1663
+1667
+1669
+1693
+1697
+1699
+1709
+1721
+1723
+1733
+1741
+1747
+1753
+1759
+1777
+1783
+1787
+1789
+1801
+1811
+1823
+1831
+1847
+1861
+1867
+1871
+1873
+1877
+1879
+1889
+1901
+1907
+1913
+1931
+1933
+1949
+1951
+1973
+1979
+1987
+1993
+1997
+1999
+2003
+2011
+2017
+2027
+2029
+2039
+2053
+2063
+2069
+2081
+2083
+2087
+2089
+2099
+2111
+2113
+2129
+2131
+2137
+2141
+2143
+2153
+2161
+2179
+2203
+2207
+2213
+2221
+2237
+2239
+2243
+2251
+2267
+2269
+2273
+2281
+2287
+2293
+2297
+2309
+2311
+2333
+2339
+2341
+2347
+2351
+2357
+2371
+2377
+2381
+2383
+2389
+2393
+2399
+2411
+2417
+2423
+2437
+2441
+2447
+2459
+2467
+2473
+2477
+2503
+2521
+2531
+2539
+2543
+2549
+2551
+2557
+2579
+2591
+2593
+2609
+2617
+2621
+2633
+2647
+2657
+2659
+2663
+2671
+2677
+2683
+2687
+2689
+2693
+2699
+2707
+2711
+2713
+2719
+2729
+2731
+2741
+2749
+2753
+2767
+2777
+2789
+2791
+2797
+2801
+2803
+2819
+2833
+2837
+2843
+2851
+2857
+2861
+2879
+2887
+2897
+2903
+2909
+2917
+2927
+2939
+2953
+2957
+2963
+2969
+2971
+2999
216,816 p3000000.txt
216,816 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
1,857,859 p30000000.txt
1,857,859 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
42 sieve.py
@@ -0,0 +1,42 @@
+
+class SieveOfEratosthenes:
+
+ def __init__(self, sieveSize):
+ self.sieveSize = sieveSize
+
+ def initialiseSieve(self):
+ self.couldBePrime = [True]*(self.sieveSize+1);
+ self.couldBePrime[0] = False
+ self.couldBePrime[1] = False
+ self.nextNumberToCheck = 0
+
+ def generatePrimes(self):
+ self.initialiseSieve()
+ pastTheSquareRootOfSize = False
+ while self.nextNumberToCheck <= self.sieveSize:
+ if self.couldBePrime[self.nextNumberToCheck]:
+ prime = self.nextNumberToCheck
+ if not pastTheSquareRootOfSize:
+ if prime*prime > self.sieveSize:
+ pastTheSquareRootOfSize = True
+ if not pastTheSquareRootOfSize:
+ self.markMultiplesNotPrime(prime)
+ yield prime
+ self.nextNumberToCheck += 1
+
+ def markMultiplesNotPrime(self, prime):
+ multipleOfPrime = prime*prime
+ while (multipleOfPrime <= self.sieveSize):
+ self.couldBePrime[multipleOfPrime] = False
+ multipleOfPrime += prime
+
+def main():
+ SIZE = 3000000
+ sieve = SieveOfEratosthenes(SIZE)
+ print ("Prime numbers up to %s:" % SIZE)
+ outfile = open ('p%s.txt' %SIZE, 'w')
+ for prime in sieve.generatePrimes():
+ outfile.write("%d\n" % prime)
+
+if __name__ == "__main__":
+ main()

0 comments on commit 63cf41e

Please sign in to comment.