Sarik_Kumpan

sieve

Feb 6th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. import math
  2. import time
  3.  
  4. def main(time):
  5.     time_temp = []
  6.     for i in range(1, time+1):
  7.         time_temp.append(sieve(2000))
  8.         with open('time.csv','w') as f:
  9.             for i in time_temp:
  10.                 f.write(str(i)+'\n')
  11.             f.close()
  12.  
  13. def sieve(max):
  14.     start = time.perf_counter()
  15.     a = [True] * (max + 1)
  16.     a[0] = a[1] = False
  17.     for i in range(2, math.floor(math.sqrt(max))):
  18.         if a[i]:
  19.             for j in range(i*i, max+1, i):
  20.                 a[j] = False
  21.     prime = []
  22.     for i in range(2, max+1):
  23.         if a[i]:
  24.             prime.append(i)
  25.     end = time.perf_counter()
  26.     time_use = (end - start)*1000
  27.     # change this to change file name
  28.     with open('K_Last.csv','w') as f:
  29.         for i in prime:
  30.             f.write(str(i)+'\n')
  31.         f.close()
  32.     return time_use
  33.  
  34.  
  35. main(100)
Advertisement
Add Comment
Please, Sign In to add comment