A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Help With Problem 1a initial=1 Denom=3 OddPrime=True PrimeNumber=0 if (Denom/2)*2==Denom: OddPrime=False print 'Number is even.' else: OddPrime=True print 'Number is odd, checking if prime..' if OddPrime==True: while PrimeNumber<1001: for i in range(initial,Denom): if Denom%i==0: OddPrime=False if OddPrime==False: Denom+=2 else: OddPrime=True if OddPrime==True: Denom+=2 PrimeNumber+=1 if PrimeNumber==1000

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

    I dont understand why this isnt working correctly. Please help

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

    Opps. Bottom should look like if PrimeNumber==1000: print Denom

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

    please paste you code on dpaste.com or pastebin.com. it'll help with debugging

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

    http://dpaste.com/543250/. There you go, any advice is appreciated

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

    added an extra period at the end of that link. http://dpaste.com/543250/ sorry.

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

    fyi, you can choose the style of the code, so you can select python, and it'll add all the pretty syntax colors

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

    http://dpaste.com/543251/ Even better, thank you

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

    Your 'initial' variable is 1, so any number divided by 1 will have 0 as a remainder. So line 14 will always be true

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

    could i fix that simply by setting 'initial' to 2, or would it be more complicated?

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

    You can, but I think there's other issues too, but that's a start

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

    ok. let's take a look. lines 3 to 11, will only be executed once. There's no loop to the beginning for it to be reused. I think you wanted the check to be done for every new Denom.

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

    well the reasoning behind that was i figured that once it was established the number was prime, and if i only added to Denom in intervals of 2, Denom would remain prime and i would not need to check it again. Am i assuming something incorrectly?

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

    I think you just want to check all odd number starting from 3. Don't get odd and prime mixed up here. You can certainly only check the odd number by incrementing by 2

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

    for line 5 to 11 since Denom=3, and doesn't every change in those lines. You can effectively remove them, since OddPrime is guarantee to be true.

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

    is your indentation on line 20 correct?

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

    i believe so, i think it just got messed up when i pasted it. Heres the revised code: http://dpaste.com/543256/

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

    ok a couple of coding style tips line 5, is not needed, since OddPrime is already True. line 10, and 14 are also not needed, because you just sent the condition in the line above to be the same as the condition.

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

    You're almost there. You have all the pieces, and a couple of extra. Just need to remove the extra stuff. consider what happens when you're in your for loop. Once you find the prime, do you stop or do you continue?

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

    by the way, don't remove line 14 just yet. You'll see why later.

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

    you'll need to play with the indentations (i.e. where the code is actually called) to get to the solution

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

    alright i have to get off the computer now, but il play with that tomorrow and see if i can figure it out. I think i have an idea of what you mean, thanks a ton for your help

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

    you're welcome. good luck. post back if you need more help

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

    Hi ntoooto. I'll attach a couple of scripts for you to have a look through. The first one only checks whether a number's prime or not, the second one defines a function for checking primes, using the code from the first, which it then uses to find the 1000th prime. I've documented each line, so they're easy to follow.

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

    I put you up a medal as well stuyboyz, keep up the good work mate.

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

    @carlsmith, I noticed that your prime_suspect.html script returns an overflow-error...when you enter a very large odd number; I was wondering, shouldn't the scripting work for any odd number regardless of its size?

  26. 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.