Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def primenumbers_brute():
- #Thanks to http://stackoverflow.com/questions/21487846/basic-prime-number-generator and http://scratch.mit.edu/projects/978284/
- x = 0
- j = 2
- chk = 1
- f = open("primes.txt", "a")
- primes = []
- notprimes = []
- ask = raw_input("how many primes? ")
- start_time = round(time.time())
- last_time = round(time.time())
- pmf = 0
- fr = open("primes.txt","r")
- # with fr as fr:
- # primes = fr.read().splitlines()
- for line in fr:
- primes.append(line)
- print(primes)
- if not(primes[-1] == "") and not(primes[-1]==2):
- j = int(primes[-1])
- # while len(primes)<int(ask):
- # k = 2
- # while not(j%int(primes[x])==0) and not(j==k):
- # k = k+1
- # if
- # if j%int(primes[x])==0:
- # j=j+1
- # x = 0
- #elif int(primes[x])>j/2:
- # j = j+1
- # x = 0
- # elif not(j%int(primes[x])==0) and int(primes[x])<j and not(j in primes):
- # primes.append(j)
- # f.write(str(j) + "\n")
- # if len(primes) >= 1000*chk:
- # elapsed_currenttime = round(time.time() - last_time)
- # last_time = round(time.time())
- # elapsed_starttime = round(last_time - start_time)
- # chk = chk + 1
- # print("There have been " + str(len(primes)) + " primes counted so far, " + str(elapsed_currenttime) + " seconds since the last message and " + str(elapsed_starttime) + " seconds from the start of the calculations.")
- while len(primes) < int(ask):
- pmf = 0
- k = 2
- while not(k==j) and not(j%k==0):
- if pmf == 0:
- k = k+1
- pmf = 1
- else:
- k = k+2
- if k == j:
- primes.append(j)
- f.write(str(j)+"\n")
- else:
- notprimes.append(j)
- if len(primes) >= 1000*chk or time.time()-start_time()%60==0:
- elapsed_currenttime = round(time.time() - last_time)
- last_time = round(time.time())
- elapsed_starttime = round(last_time - start_time)
- chk = chk + 1
- print("There have been " + str(len(primes)) + " primes counted so far, " + str(elapsed_currenttime) + " seconds since the last message and " + str(elapsed_starttime) + " seconds from the start of the calculations.")
- j = j + 1
- if j % 2 == 0:
- j = j + 1
- else:
- j = j
- print("Primes written to file 'primes.txt', " + str(len(primes)) + " written in " + str(round(time.time() - start_time)) + " seconds")
- f.close
- return(" ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement