Advertisement
vaboro

ps1b.py

Apr 18th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1.  
  2. ##################################################################################################################################
  3. # Note, that the first prime in the set of primes is 2. Therefore, when we start checking the next prime candidate by dividing   #
  4. # it by all the previous primes, the first prime we divide the cadidate by is 2 and, thus, first, we check if the number is even #
  5. # or odd. If it's even, then the candidate is rejected and we start checking the next candidate.                                 #
  6. ##################################################################################################################################
  7.  
  8. import math
  9.  
  10. print "The following program computes the sum of the logariths of all the primes from 2 to some number n and prints out the sum",
  11. print "of the logs of the primes, the number n, and the ratio of these two quanities."
  12.  
  13. UserNumber = int(raw_input("Please, enter number n. It should be integer:")) #at some point in the program I need to make sure my next prime is > than n
  14.  
  15. Primes = [2] #stores all computed primes
  16.  
  17. NumberOfPrimes = len(Primes) #holds the number of primes in the list Primes
  18.  
  19. n = Primes[NumberOfPrimes - 1] + 1 #next prime candidate
  20.  
  21. #print "UserNumber =", UserNumber, "First prime candidate =", n
  22.  
  23. while n < UserNumber:
  24.  
  25.     #print "UserNumber =", UserNumber, "Next prime candidate =", n
  26.    
  27.     #n = Primes[NumberOfPrimes - 1] + 1 #next prime candidate
  28.  
  29.     i = 0
  30.  
  31.     while i < len(Primes) and Primes[i] <= int(math.sqrt(n)): #divide the cadidate by all the previous primes up to the square root of the candidate
  32.        
  33.         #print "Prime candidate",n,"i is",i
  34.  
  35.         if n % Primes[i] == 0:
  36.  
  37.             n = n + 1          #if there's a remainder choose the next candidate
  38.            
  39.             i = 0              #reset counter to start checking from the beginning of the set of primes
  40.            
  41.         else:
  42.  
  43.             i = i + 1 #continue checking primality of the candidate with next prime in the set of primes
  44.  
  45.     Primes.append(n) #the candidate couldn't be divided without remainder by any of the previous primes, the candidate is prime, append it to the list
  46.  
  47.     NumberOfPrimes = len(Primes) #update the number of primes
  48.  
  49.     n = Primes[NumberOfPrimes - 1] + 1 #next prime candidate
  50.  
  51.     #print Primes
  52.  
  53. #print "The 1000th prime is", Primes[999]
  54. #print "The 2000th prime is", Primes[1999]
  55. #print "The 3000th prime is", Primes[2999]
  56. #print "The 4000th prime is", Primes[3999]
  57. #print "The 5000th prime is", Primes[4999]
  58. #print Primes
  59.  
  60. SumOfLogs = 0
  61.  
  62. for i in range(len(Primes)):
  63.     SumOfLogs = SumOfLogs + math.log(Primes[i])
  64.  
  65. print "Sum of the logs of primes =", SumOfLogs, "User number n =", UserNumber, "Ratio of the sum of logs to user number =", SumOfLogs/n
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement