Skip to content

Commit

Permalink
prime.py firstrun finds first 1,000,000th Prime 15485863 something to…
Browse files Browse the repository at this point in the history
… work with.

Signed-off-by: Damian Williamson <[email protected]>
  • Loading branch information
Willtech committed Jan 9, 2024
2 parents 53d8bc3 + 39d7596 commit 35de353
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 4 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
A Prime is any number only divisible by 1 and itself searching first that a number is not even if it divides by 2, optimising using a "Vignette Sieve" that if not only up to n/2 needs to be tested as a divisor, then 3 if not only up to n/3 needs to be tested as divisor, n/5, n/7, n/11, n/13, n/17, n/19, n/23, n/29, n/31 & so on so that only divisibility by a prime needs to be checked for to prove not prime & since for example we have ruled out that the number is a factor of three or two so there is no natural number greater than n/3 that can be a divisor.

### Usage
On first run use `prime.py firstrun` to build the data table. The data table *may be* provided. It can find up to 104729 the 10,000th Prime in 6 seconds and can be faster with `mpiexec`

On first run use `prime.py firstrun` to build the data table. The data table *may be* provided.
```log prime.py firstrun
Initialise...
Firstrun: Building _data
Completed creating data to 10000 Primes
Largest Prime in Index:104729
```

`prime.py [number to check]`

Expand All @@ -22,3 +28,4 @@ It should be possible to run with `mpiexec` [Build a Raspberry Pi cluster comput
Professor. Damian A. James Williamson

[1]: https://magpi.raspberrypi.com/articles/build-a-raspberry-pi-cluster-computer

1 change: 1 addition & 0 deletions _data

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions notes
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
setup

input number
input whether to fill missing primes

first run no input fill data file up to some n

file read
file write
is_prime
from file
add to file
from file check up to fraction
add to file fraction bigger than file check all not divisible by file up to fraction

If prime output and add to file.
If prime output
108 changes: 108 additions & 0 deletions prime.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,115 @@
# Source Code produced by Willtech 2023
# v0.1 hand coded by HRjJ

#prime.py [number to check] []
import time
tt = time.time()

print("Initialise...")
## setup dependencies
import sys
import re

from mpmath import *
mp.dps = 15000000000
#@manual{mpmath,
# key = {mpmath},
# author = {The mpmath development team},
# title = {mpmath: a {P}ython library for arbitrary-precision floating-point arithmetic (version 1.3.0)},
# note = {{\tt http://mpmath.org/}},
# year = {2023},
#}

#Imput handler
try:
if sys.argv[1] is not None:
if sys.argv[1] == "firstrun":
x = -1
else:
try:
mp.dps = len(str(int(sys.argv[1])))
x = mpf(str(sys.argv[1]))
except:
exit ("Input error.")
if x < 1:
x = x * -1
except:
mp.dps = 185
x = mpf('531137992816767098689588206552468627329593117727031923199444138200403559860852242739162502265229285668889329486246501015346579337652707239409519978766587351943831270835393219031728127')
if x == 0:
exit('0 will not be prime');

#Functions
def fileread(primes):
primefile = open("_data", "r")
primes = primefile.read().split(",")
primefile.close
return primes

def filesave(data):
content = str(data)
primefile = open("_data", "w")
primefile.write(content)
primefile.close

def findprime(x, i):
j = mpf(x) / i
if str(j).split(".")[1] == "0":
# printoutput (x, i, j, 'Doh!') #
return (False)
else:
# printoutput (x, i, j, 'Ding!') #
return (True)

def printoutput(x, i, j, f):
print ('Dividend:', x)
print ('Divisor:', i)
print ('Quotient:', j, 'from divisor:', i, f)
print ('---')

def printfound(x):
print ('Found:', x)
print ('------')

def createint(i):
return(int(i))

#Development proof
#primes = ""
#primes = fileread(primes)

#Runtime
if x == -1: #firstrun
print ("Firstrun: Building _data")
arp = 1
primes = ""
primes = 1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71
mp.dps = mpf(len(str(primes[len(primes)-1])))
while len(primes) <= 1000000:
arp = 1
x = mpf(str(primes[len(primes)-1]+2))
mp.dps = len(str(x))
if findprime(x, 2):
i = createint(primes[arp])
while i < x / i:
i = createint(primes[arp])
if findprime(mpf(x), i):
arp = arp + 1
else:
arp = 1
x = x + 2
printfound(x) #
primes = (*primes, int(x))
else:
print (primes)
print ('Looking for', x)
exit ('Math error creating _data.');

filesave(primes)
fileread(primes)
# print(primes) #
print ('Completed creating data to', len(primes)-1, 'Primes');
print ('Largest Prime in Index:' + str(int(''.join(filter(str.isdigit, str(primes[len(primes)-1]))))))
exit ("--- %s Seconds ---" % (time.time() - tt))

#Main

0 comments on commit 35de353

Please sign in to comment.