Advertisement
L0neGamer

Reading and Writing Primenumbers

Jun 10th, 2014
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.63 KB | None | 0 0
  1. def primenumbers_brute():
  2.     #Thanks to http://stackoverflow.com/questions/21487846/basic-prime-number-generator and http://scratch.mit.edu/projects/978284/
  3.     x = 0
  4.     j = 2
  5.     chk = 1
  6.     f = open("primes.txt", "a")
  7.     primes = []
  8.     notprimes = []
  9.     ask = raw_input("how many primes? ")
  10.     start_time = round(time.time())
  11.     last_time = round(time.time())
  12.     pmf = 0
  13.     fr = open("primes.txt","r")
  14. #    with fr as fr:
  15.  #       primes = fr.read().splitlines()
  16.     for line in fr:
  17.         primes.append(line)
  18.     print(primes)
  19.     if not(primes[-1] == "") and not(primes[-1]==2):
  20.         j = int(primes[-1])
  21. #    while len(primes)<int(ask):
  22.  #       k = 2
  23.   #      while not(j%int(primes[x])==0) and not(j==k):
  24.    #         k = k+1
  25.     #        if
  26.      #   if j%int(primes[x])==0:
  27.       #      j=j+1
  28.        #     x = 0
  29.         #elif int(primes[x])>j/2:
  30.          #   j = j+1
  31.           #  x = 0
  32. #        elif not(j%int(primes[x])==0) and int(primes[x])<j and not(j in primes):
  33.  #           primes.append(j)
  34.   #          f.write(str(j) + "\n")
  35.    #     if len(primes) >= 1000*chk:
  36.     #        elapsed_currenttime = round(time.time() - last_time)
  37.      #       last_time = round(time.time())
  38.       #      elapsed_starttime = round(last_time - start_time)
  39.        #     chk = chk + 1
  40.         #    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.")
  41.     while len(primes) < int(ask):
  42.         pmf = 0
  43.         k = 2
  44.         while not(k==j) and not(j%k==0):
  45.             if pmf == 0:
  46.                 k = k+1
  47.                 pmf = 1
  48.             else:
  49.                 k = k+2
  50.         if k == j:
  51.             primes.append(j)
  52.             f.write(str(j)+"\n")
  53.         else:
  54.             notprimes.append(j)
  55.         if len(primes) >= 1000*chk or time.time()-start_time()%60==0:
  56.             elapsed_currenttime = round(time.time() - last_time)
  57.             last_time = round(time.time())
  58.             elapsed_starttime = round(last_time - start_time)
  59.             chk = chk + 1
  60.             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.")
  61.         j = j + 1
  62.         if j % 2 == 0:
  63.             j = j + 1
  64.         else:
  65.             j = j
  66.     print("Primes written to file 'primes.txt', " + str(len(primes)) + " written in " + str(round(time.time() - start_time)) + " seconds")
  67.     f.close
  68.     return(" ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement