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

PS 1.2 - The last three lines of my code doesn't seem to be executing. I was wondering if somebody could help me with this. Thanks in advance. import math #Variables n = input('Enter a prime number: ') counter = 0 candidate = n divisor = 2 primelist = [2] sum = 0 while candidate != 0: while candidate % divisor != 0: divisor += 1 if divisor == candidate: primelist.append(candidate) print primelist candidate -= 2 counter += 1 divisor = 2 else: candidate -= 2 divisor = 2 for num in primelist: sum = sum + num

OpenStudy (anonymous):

import math #Variables n = input('Enter a prime number: ') counter = 0 candidate = n divisor = 2 primelist = [2] sum = 0 while candidate != 0: while candidate % divisor != 0: divisor += 1 if divisor == candidate: primelist.append(candidate) print primelist candidate -= 2 counter += 1 divisor = 2 else: candidate -= 2 divisor = 2 for num in primelist: sum = sum + num print 'Sum of all those logs:',sum

OpenStudy (anonymous):

For some reason it didn't post all the code the first time. Sorry about that

OpenStudy (anonymous):

I'm not sure why its not executing, but if I remember this assignment correctly it should be sum += log(num), n doesn't have to be a prime number, you're supposed to output the ratio of sum to n as well as the sum, and the last print command shouldn't be indented into the for loop.

OpenStudy (anonymous):

When I run it as typed, without import math, (you're not currently doing anything that requires the math module, but when you add the log function as JacobPaige noted, you will need math) it works. first, you can simplify your variables a little bit by saying candidate = input('Enter a prime number: ') and not using that intermediate n variable. It looks like you're worried that assigning that way might cause the program to ask the user for a number every time it needs the candidate variable, but it won't do that. That statement just assigns to the integer, not to the function raw_input. It looks to me like you've tried to reverse a solution to the original primes problem by starting at a high prime number and working down from there. I wouldn't do it that way. First of all, there's no requirement in the problem statement that says the upper limit number must be prime, so an approach that only works when given a prime number is a little limited. Second, I think there's a way to take the original solution that I presyume you have and run it normally, with an added step to check the number you're testing to make sure it's less than the upper limit. Then you can add the logs of a list of primes basically the way you're doing it now, using the log function as JacobPaige suggested.

OpenStudy (anonymous):

First of all, thank you both for the replies. They are both very helpful. Yeah I didn't have the log function in there yet because I was just trying to get the summing to work first. But now the summing works and i believe i have finally worked out the problem set woo!!!!! Thank you so much!!!!!

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!
Latest Questions
Lilmunchin: Trump or Biden
50 seconds ago 17 Replies 1 Medal
ARTSMART: Art!
48 minutes ago 5 Replies 4 Medals
Jasonisyours: What were the key causes of the French Revolution in 1789?
42 minutes ago 3 Replies 5 Medals
PureSoulless: Why is the word "Pedophile" always censored in yt vids?
1 day ago 3 Replies 0 Medals
Jalli: What's 58x3634u00b07
22 hours ago 6 Replies 3 Medals
arriya: who wanna play roblox
1 day ago 5 Replies 1 Medal
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!