Problem Set 1, Problem 2: Question on comparison of methods. So I found PS1 P1 to be very doable, especially by being able to look over s-kmarti19 and siili's examples (thanks!). Problem 2 ended up giving me fits though, paradoxically. I'm still having a lot of trouble with indents, loops, and placing statements at the right indent. Anyway, I now have a functioning solution for Problem 2 (http://codepad.org/wVOsg7Q5) based on my Problem 1 solution (http://codepad.org/bP19lwSi) (much of it cribbed from siili). Continued in reply (I'm verbose).
The output for my solution to Problem 2 is: Enter a number: 1009 You entered: 1009 Sum of logarithms of all primes from 2 to 1009 = 963.16198014 The ratio of the sum of the logarithms to 1009 = 0.954570842557 I also wrote an alternate solution to this problem using s-kmarti19's method of using a for...in loop with xrange(2,sqrt(n)) ( http://codepad.org/eodgmsKE). The output from this code is: Enter a number: 1009 You entered: 1009 Sum of logarithms of all primes from 2 to 1009 = 956.24526512 The ratio of the sum of the logarithms to 1009 = 0.947715822716 Where is the difference in the sum of logarithms between the solution to Problem 1 and Problem 2 coming from? I'm also curious why you set logsum = 0.3109996 initially? This alone shouldn't make much of a difference since it's only holding the place of math.log(2)... Does it have to do with using sqrt(x) in the xrange?
Your first solution is taking into account a prime bigger than the number entered.
for primes >2 & <1009 i get: 956.24526512 & 0.947715822716 for primes >2 & <1010 i get: 963.16198014 & 0.953625722911
After you enter the loop, you count 1000 numbers, but you forgot that the number 2 had already been calculated. So, you should loop 999 times instead of 1000.
Ah! Ok, thank you. I was not accounting for the case in which n (the number entered) is prime. I just moved the update of the log sum variable inside the prime < number entered 'if' statement. My updated code: http://codepad.org/yrkwgNhY
Join our real-time social learning platform and learn together with your friends!