I've been learning Python 2.5.4 and I have the following problem to solve:
"Write a program that computes the sum of the logarithms of all the primes from 2 to some number n, and print out the sum of the logs of the primes, the number n, and the ratio of these two quantities. Test this for different values of n."
This is what I have so far:
from math import *
n = raw_input('This is a logarithm ratio tester. Which number do you want to test? ')
for x in range(2,n): #picks numbers to test
for divisor in range(2, 1+int(sqrt(x+1))):
if x%divisor == 0: #checks if x is prime
log(x) #computes log of prime
Unfortunately I'm not sure how to implement a function for summing all the logs. I would imagine that once I have that, I just need to conclude the program with:
print 'Sum of the logs of the primes is',logsum
print 'n =',n
print 'Ratio of sum to n is',logsum/n
Or some variant. But what would be a good way to obtain what I've labeled logsum? Please note I have been studying programming for no more than a week, I know very few statements/functions by heart, and I am not a mathematician. When in doubt, assume I'm an idiot. Thanks!
I would wrap some of your code in functions, and fix your definition of prime:
def isprime(x):
for j in range(2,1 + int(sqrt(x - 1))):
if x%j == 0:
return False
return True
def primes(n):
return [j for j in range(2, n) if isprime(n)]
logsum = 0
for p in primes(n):
logsum += log(p)
You should check to see if the number is prime before calculating its log and then add the value of log(x) to a variable logsum like this:
from math import *
logsum = 0
n = raw_input('This is a logarithm ratio tester. Which number do you want to test? ')
for x in range(2,n): #picks numbers to test
isprime = True
for divisor in range(2, 1+int(sqrt(x+1))):
if x%divisor == 0: #checks if x is prime
isprime = False #computes log of prime
break
if isprime:
logsum += log(x)
Also, since you're checking a lot of values, maybe implementing The Sieve of Eratosthenes would be a nice way to continue learning.
EDIT: Using Juri Robl's suggestion, the code could be simplified to:
from math import *
logsum = 0
n = raw_input('This is a logarithm ratio tester. Which number do you want to test? ')
for x in range(2,n): #picks numbers to test
for divisor in range(2, 1+int(sqrt(x+1))):
if x%divisor == 0: #checks if x is prime
break
else:
logsum += log(x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With