A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Problem Set 1.1 ends with a non-prime number? My Code: # Problem Set 1 # Name: Cody Dostal # Collaborators: None # Time: 0:30 # PotentialPrime = 1 divider = 2 primecount = 1 while primecount < 1000: PotentialPrime = PotentialPrime + 2 divider = 2 isnotprime = 0 while divider <= (PotentialPrime/2) and isnotprime == 0: if PotentialPrime%divider == 0: isnotprime = 1 else: divider = divider + 1 if isnotprime == 0: primecount = primecount + 1 print "Potential 1000th prime: " + str(PotentialPrime) + " " + "Prime Count: "

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

    It ends with 7919, which 7919/3 = 2639 and 2639 * 7917 = Woooooooah? hahah

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

    I mean, 2639 * 3 = 7917 = Woaaaaaaaaaaah?

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

    7919/3 is not exactly 2639. It is 2639.6666667. There is a remainder, so is not the same as 2639.0*3.

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

    Yeah, 7919 is correct. Good job! Also, if you dedent the print line all the way out of the first while statement, it will only print the last number. Aslander was pointing out that the way integer division works isn't the way division of real numbers(floats) works. according to integer division, for instance, 5/3 = 1. If you check specifically, you'll find that it's actually 1 modulo(meaning "with the remainder") 2, but the basic thing to understand is that if you divide an integer by another integer, you'll ALWAYS get another integer, regardless of what the answer would be if you were using real numbers.

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

    Thanks. That's crazy, I forgot Python is like that... So if I wanted to show it is prime, I would have to do 7919.0/3? Would that end with showing it is a prime? Thanks!

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

    I'm not sure what you mean by "showing it is a prime." Why would trying to divide it by 3 show that it's prime?

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

    I mean that, to show it is not divisible by 3 as well? I just want to know that real quick for future programs xD

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

    well, if you want to know that a number is NOT evenly divisible by another number, you can check the remainder. So the statement x%3 != 0 would return False if x was evenly divisible by 3 because if it was, the remainder would be 0. the % sign (called the modulo) returns the remainder from integer division.

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

    n4s93: I think you could simply remove one of your IF statements, and add an else statement instead. You don't really need the MAX number either, as you can simply make: while len(primes) < 1000: As for the rest of your idea. Think about it this way - with regards to your code: if i / d has a remainder, then add one to your divisor, until you reach i. In that case, return i as prime, and append your primes list. For either case, you will add a number to both i and d. Since a prime can only be odd, once you find your first prime after 2, you can just add 2 to your prime, and start the sequence all over again for your next i, with d = 2.

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

    @somnamniac: Thank you so much :) I'll edit my code when I'm home and see how that works out :) @Aslander: Huh? hahha

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

    lol - ya for some reason I replied on your question when it was for someone else :-) Sorry dude! Although it is on the same subject...

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

    Yeah haa. I got very confused because of the MAX statement not being in my code, :) That definitely would help, however :)

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