A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

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

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.