Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- import time
- import sys
- import math
- try:
- import numpypy
- except:
- pass
- import numpy
- def gcd(x,y):
- if x%y == 0:
- return y
- return gcd(y,x%y)
- def build_sieve(n):
- nums = numpy.arange(2,n+1,dtype=numpy.int32)
- primes = []
- ct = int(math.sqrt(n))
- sp = int(ct/50)
- x = 2
- count = 0
- tick = 0
- print("\r[%50s]"%("="*tick),end="")
- while x <= ct:
- if x >= tick*sp+sp:
- print("\r[%50s]"%("="*tick),end="")
- tick += 1
- sys.stdout.flush()
- nums = nums[nums%x != 0]
- primes.append(x)
- x = nums[0]
- print("\r"+" "*53,end="\r")
- for x in nums:
- primes.append(x)
- return primes
- start = time.time()
- primes = build_sieve(int(raw_input("> ")))
- elapsed = time.time() - start
- print("You took %f seconds to calculate %d primes"%(elapsed,len(primes)))
- yeses = set(["yes","y","yeah","yep"])
- noes = set(["no","n","nope"])
- res = "huh"
- while res not in noes and res not in yeses:
- res = raw_input("Would you like to save to a file [y/n]? ").lower()
- if res not in noes and res not in yeses:
- print("Please type y or n")
- if res in yeses:
- fname = raw_input("File Name: ")
- f = open(fname,"w")
- count = 0
- l = len(primes)
- sp = int(l/3000)
- for x in primes:
- f.write(str(x))
- f.write("\n")
- if int(count)%sp == 0:
- print("\r%5.1f%%"%(100.0*count/l),end="")
- sys.stdout.flush()
- count += 1
- print("\r"+" "*50,end="\r")
- f.close()
- else:
- print("Okay then, goodbye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement