anonymous
  • 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
MIT 6.00 Intro Computer Science (OCW)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions.

anonymous
  • 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
MIT 6.00 Intro Computer Science (OCW)
jamiebookeater
  • jamiebookeater
See more answers at brainly.com
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your free account and access expert answers to this
and thousands of other questions

anonymous
  • 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
anonymous
  • anonymous
For some reason it didn't post all the code the first time. Sorry about that
anonymous
  • 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.

Looking for something else?

Not the answer you are looking for? Search for more explanations.

More answers

anonymous
  • 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.
anonymous
  • 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!!!!!

Looking for something else?

Not the answer you are looking for? Search for more explanations.