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

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

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

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.

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.

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!!!!!

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