Advertisement
Night_Wood

Primes

Jan 15th, 2016
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. # See 'foundPrimes.txt' for what is loaded/saved in this code on each run
  2.  
  3. def isPrime(integer):
  4.     global primes;
  5.    
  6.     for i in range(0,len(primes)):
  7.         if(integer%int(primes[i])==0):
  8.             return False;
  9.        
  10.     return True;
  11.  
  12. def printList(list):
  13.     elements = 0;
  14.     for i in list:
  15.         print i,
  16.         if(elements==10):
  17.             elements = 0;
  18.             print;
  19.         elements += 1;
  20.  
  21. def listToString(list):
  22.     output = "";
  23.     for i in list:
  24.         output += str(i) + " ";
  25.     return output[0:len(output)-1];
  26.  
  27. print;
  28.  
  29. foundPrimes = open('foundPrimes.txt', 'r');
  30. print 'Previously found primes:';
  31. primes = foundPrimes.read().split(' ');
  32. printList(primes);
  33. print;
  34. foundPrimes.close();
  35.  
  36. start = int(primes[len(primes)-1]);
  37. limit = 100001;
  38.  
  39. for i in range(start, start+limit, 2):
  40.     if(isPrime(i)):
  41.         primes.append(i);
  42.        
  43. print '\nUpdated found primes:';
  44. printList(primes);
  45.  
  46. foundPrimes = open('foundPrimes.txt', 'w');
  47. foundPrimes.write(listToString(primes));
  48. foundPrimes.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement