Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

Hi everyone. I'm doing the second part of Problem Set 1, and I have some code started for how to compute the sum of the logarithms of all the primes from 2 to some number n, but I am stuck at a certain point (see below): 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 How do I create variable logsum for the sum of all logs for prime values of x?

OpenStudy (anonymous):

After I have that variable set up, then I assume I just need to do: print 'Sum of the logs of the primes is',logsum print 'n =',n print 'Ratio of sum to n is',logsum/n And that will complete the rest of the instructions. Right?

OpenStudy (anonymous):

Your beginning and ending points look on point to me. The one thing is though, under your x%divisor is where you what to track your logsum variable. So it should look like: from math import * n = raw_input('This is a logarithm ratio tester. Which number do you want to test? ') *****logsum = 0.0 #Initialize your logsum variable at zero. This will be a float type variable***** 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 **** logsum += log(x) #adds the log of the current prime number to the current sum of all the logs leading up to this point **** Then you're good to go from there

OpenStudy (anonymous):

(Of course, take the asterisks out of the code- I included those just to highlight the parts I added/modified in your original)

OpenStudy (anonymous):

That's great help, thanks. I did now wind up with a type error from using a string for the raw_input value, but I corrected it by using n = int(raw_input('This is a logarithm ratio tester. Which number do you want to test? ') so now everything is great. Thanks!

OpenStudy (anonymous):

Smart people lol

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!