Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ##################################################################################################################################
- # Note, that the first prime in the set of primes is 2. Therefore, when we start checking the next prime candidate by dividing #
- # 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 #
- # or odd. If it's even, then the candidate is rejected and we start checking the next candidate. #
- ##################################################################################################################################
- import math
- 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",
- print "of the logs of the primes, the number n, and the ratio of these two quanities."
- 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
- Primes = [2] #stores all computed primes
- NumberOfPrimes = len(Primes) #holds the number of primes in the list Primes
- n = Primes[NumberOfPrimes - 1] + 1 #next prime candidate
- #print "UserNumber =", UserNumber, "First prime candidate =", n
- while n < UserNumber:
- #print "UserNumber =", UserNumber, "Next prime candidate =", n
- #n = Primes[NumberOfPrimes - 1] + 1 #next prime candidate
- i = 0
- 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
- #print "Prime candidate",n,"i is",i
- if n % Primes[i] == 0:
- n = n + 1 #if there's a remainder choose the next candidate
- i = 0 #reset counter to start checking from the beginning of the set of primes
- else:
- i = i + 1 #continue checking primality of the candidate with next prime in the set of primes
- 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
- NumberOfPrimes = len(Primes) #update the number of primes
- n = Primes[NumberOfPrimes - 1] + 1 #next prime candidate
- #print Primes
- #print "The 1000th prime is", Primes[999]
- #print "The 2000th prime is", Primes[1999]
- #print "The 3000th prime is", Primes[2999]
- #print "The 4000th prime is", Primes[3999]
- #print "The 5000th prime is", Primes[4999]
- #print Primes
- SumOfLogs = 0
- for i in range(len(Primes)):
- SumOfLogs = SumOfLogs + math.log(Primes[i])
- 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