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

Hello, I am having difficulty editing my solution for ps1a into a solution for ps1b. I had no problem with the ps1a, but this second part has left me completely clueless.. Any help will be appreciated, thanks.

OpenStudy (anonymous):

Here's my code for ps1a:

OpenStudy (anonymous):

First what you need to do is add the the command import math to the top of your code then add an assignment named sumOfPrimes=0. Under your if true statement add the command sumOfPrimes=sumOfPrimes+math.log(testInteger). Now depending on through what number of primes you want to add you need to change your number in the while loop.

OpenStudy (anonymous):

I believe this is how you do it but I did it wrong the first time I did it because I misread the directions and I didn't want to redo it. So if anyone else has any suggestions please answer back.

OpenStudy (anonymous):

I added some lines into your script. It should work. If any questions, feel free to ask me. ## Start from here from math import* testInteger = 1 #Number to be tested if prime numberOfPrime = 1 #Number of prime (changes if prime # found) lnproduct=log(2) n=300 while(numberOfPrime<n): #Sets limit, stops when 1000th prime found isPrime = True #assume that the test number is prime for x in range(2,testInteger/2): #if any number between 2 and the number divides evenly if testInteger%x ==0: #Then the number is not prime isPrime = False if isPrime == True: lnproduct=lnproduct+log(testInteger) numberOfPrime = numberOfPrime + 1 #numbers the prime testInteger = testInteger + 2 #submits next number to test if isPrime == False: testInteger = testInteger + 2 #if not prime, tests next number n=testInteger ratio=lnproduct/n print lnproduct print n print ratio

OpenStudy (anonymous):

Thanks, I ran that code and it seemed to work great, plus I understand the logic behind each addition. I appreciate the help!

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!