Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import time,ctime,sleep
- DBFILE="primesDB.txt"
- primes=[2,5] #Not found by fastPrime(n) because of a speed optimization
- autosv=1000000 #Autosave not included this paste
- #
- # LOAD FROM DATABASE DISABLED!
- #
- def isprime(n): ## For imports for example?
- if n < 2: return str(n)+" is not a prime number."
- if n == 2: return str(n)+" is a prime number."
- for i in [2]+range(3,int(n**0.5)//1+1,2):
- if not n%i:
- return "%i is %i*%i"%(n,i,n/i)
- else:
- return "%i is not a prime number."%(n)
- return "%i is a prime number."%(n)
- def fastPrime(n):
- if n%10 in [1,3,7,9]:
- for i in range(3,int(n**0.5)//1+1,2):
- if not n%i:
- #return "%i is %i*%i"%(n,i,n/i) #Slower
- return str(n)+" is "+str(i)+"*"+str(n/i)#Faster
- else:
- #return "%i is not a prime number."%(n) #Slower
- return str(n)+" is not a prime number."#Faster
- primes.append(n)
- #return "%i is a prime number."%(n) #Slower
- return str(n)+" is a prime number."#Faster
- def check(start=3, end=1000000):
- s=time()
- if start<3:
- start=3
- if end<start:
- end=1000000
- for i in range(start,end):
- #curr=isprime(i)
- curr=fastPrime(i)
- if curr==str(i)+" is a prime number.":
- #print "Found a prime: %i; %s"%(i,ctime(time())) #SLOWER! In all programs that use a lot of string logic use string concat. instead of %x
- print "Found a prime: "+str(i)+"; "+ctime(time())
- print "Finished. Took %.2f seconds to finish."%(time()-s)
- if __name__=="__main__":
- print "Tip: '-1' to ignore (Replaced by defaults: Start=3, End=1000000)."
- check(input("Start at which number?"),input("End at which number?"))
- print "Primes total found: %i"%(len(primes))
- print "Pre-save..."
- p=[]
- for i in primes:
- p.append(str(i))
- sv="\n".join(p)
- with open(DBFILE,"w") as f:
- print "Saving..."
- f.write(sv)
- print "Saved!"
- raw_input("Enter to continue...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement