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

PSET1 part 2: Does anyone know the formula for the ratio? I know it asks for the ratio for the sum of primes to n, but mine doesn't get close to 1. It gets close to 1 only when I take the ratio for the sum of primes to the log(n).

OpenStudy (anonymous):

hi - the ratio you're looking to program is: n over the sum of the logs of the primes less than n

OpenStudy (anonymous):

from math import *#this code includes a check-up b=1 while(b!=0): b=int(raw_input("number")) x=3 n=2 y=log(2)+log(3) while (n<b): x=x+2 a=x-2 while((x%a)!=0): if (a>3): a=a-2 else: y=y+log(x) a=1 n=n+1 print x y=y-log(x) print y print (y/x)#in the beginning of RUN press ENTER(if there is an empty space) You can make some comparisons here.Good luck!

OpenStudy (anonymous):

Thanks for the help. =/ my problem was I misinterpreted what n was. I thought I had to take the prime numbers less than n, not n prime numbers.

OpenStudy (anonymous):

n is b(th) prime number ,as for the formula it should be \[\sum_{i=1}^{b}\log(x _{i})\le n\] from this equation we can conlude that the ratio of sum to n is less than 1.That means the max value of ratio is 1

OpenStudy (anonymous):

I think if you will look into my code it would clear and easy(assuming you know how the code works)

OpenStudy (anonymous):

That's how I realized I assumed n was wrong. On a side note, it would've been much easier to follow if you used some better variable names. Thanks for the help.

OpenStudy (anonymous):

It is meant to be prime numbers less than n, Not the nth prime number. you were correct the first time. attached is my code, see if it helps

OpenStudy (anonymous):

I didn't try your code MRE2010 but I followed your comments and pretty much my idea was similar. Find all primes less than n. add log of all the primes. take the ratio of primes to n. My issue was that it wasn't approaching n with bigger values. ..going to check code again =/

OpenStudy (anonymous):

=) I'm glad it wasn't too hard to get back to how I had it in the beginning. I tested with LARGER numbers and it follows theorem. Thanks MRE2010.

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!