I'm struggling with the sum of the logs problem in Problem Set 1. My ratio seems to approach 0, and very slowly. I'm not getting the output I expected from reading the problem.
from math import * n = 10 oddinteger = 3 checkdigit = 2 primecount = 1 primelogsum = log(2) while n < 10000: while primecount < n: if oddinteger%checkdigit == 0: oddinteger += 2 checkdigit = 2 elif oddinteger%checkdigit != 0 and checkdigit*2 < oddinteger: checkdigit += 1 else: primecount += 1 primelogsum += log(oddinteger) oddinteger += 2 checkdigit = 2 print "Sum of the logs of the primes: " + str(primelogsum) print "n: " + str(n) print "Ratio of sum of the logs of the primes to the value n: " + str(float(primelogsum)/float(n)) print "" primelogsum = log(2) n += 10
what does n represent and what is it meant to be doing?
Well, I'm a moron. I was operating under the assumption that: as n increases - primelogsum approaches 1. when it now seems that I need to keep taking the sum of the logs each time I get a new prime number, and that ratio will approach 1.
while leaving n as a constant
actually now that I read it again, I think I was right - n is supposed to increase with each iteration. I set it to some value, then add up all the logs of prime numbers up to n, divide that result by n, and then increase n and have another iteration.
Are you actually adding up ALL the logs? Why does primelogsum keep getting reset to log(2)?
I'm doing that each time the while primecount < n condition becomes false, which begs the question, why do I care whether my primecount is < n? I should be checking my oddinteger that is being tested for primeness against n.
... and we're approaching 1 on a squiggly line :-)
cool!
Thanks for the help!
Join our real-time social learning platform and learn together with your friends!