A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Help on p1?

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

    So I have this code so far... count=1 number=3 divisor=2 while(count<1000): (while (divisor<number): if (number%divisor==0): print number, "is not a prime" divisor=2 number=number+2 else: divisor=divisor+1 if(divisor==number): count=count+1 print number, "is a prime" number=number+2 divisor=2 print number From what I can tell (by the print statements in the if/else), it is correctly finding the prime numbers, but I can't get the count to work. i.e. the loop won't stop. Any suggestions?

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

    Nevermind! Got it!

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

    what was it ? share the code please !

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

    I just inserted a break statement at the end of the inner while loop. I also had to change the output to print number-2 (the end of the loop increases number, even in the last loop). The new code looks like #File: ps1_1.py # To print the 1000th prime number count=1 number=3 divisor=2 while(count<1000): while (divisor<number): # Number is not prime if (number%divisor==0): divisor=2 number=number+2 # Number is prime else: divisor=divisor+1 if(divisor==number): count=count+1 number=number+2 divisor=2 break print number-2 However, this seems to take awhile to calculate so I'm trying to see if there's a better way to write this

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

    https://gist.github.com/944461 Try reading through this, it should help you out no end.

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

    here's a good example: #Determine the 1000th prime number candidate=1 #Already know that 2 is prime primeCount=1 while (primeCount<=1000): isPrime="true" i=2 halfCand=(candidate/2) while (isPrime=="true") and (i<=halfCand): if ((candidate%i)==0): isPrime="false" else: i+=1 if isPrime=="true": print(candidate, "is a prime.") primeCount+=1 #else: #print(candidate, " is not a prime.") candidate+=2 print("The 1000th prime number is ",(candidate-2))

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

    How about = = = = = = = = = = = def is_prime(number): if number < 2: return False for each in range(2, (number+2) / 2): if number % each == 0: return False return True tally, n = 0, 0 while tally < 1000: n += 1 if is_prime(n): tally += 1 print n = = = = = = = = = = = =

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

    looking good, :D

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

    would explain this when u have a chance: for each in range(2, (number+2) / 2): I see that "(number + 2) " is divided by 2, but what is that first 2 in the expression doing?

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

    Ah, that ~ it just makes sure the value that range() will run up to is always just more than half of number. We could just do for each in range(2, number): but it's more efficient to do the range from 2, up to not-less-than-half-of-number (because, more than half of number, times by 2 (or more than 2) can never produce number, it'll always be too big). This way, we only try about half as many values for the variable `each` and so we get our result about twice as fast, but still get the right result. It's a bit of a sketchy way to do it, but it works. Welcome to the world of 'quick hacks'. Well spotted though :)

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

    I wrote this earlier to help explain it to some one else, it is the above, but with a ton of comments that walk you through it. If anyone fancies it https://gist.github.com/944461 You'll have to copy and paste it, I think because it's secure http, but that's just the way github does their URLs.

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

    well, i'll understand it better, by and by... :D

  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.