A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
Help on p1?
anonymous
 5 years ago
Help on p1?

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0So 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?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0what was it ? share the code please !

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I just inserted a break statement at the end of the inner while loop. I also had to change the output to print number2 (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 number2 However, this seems to take awhile to calculate so I'm trying to see if there's a better way to write this

carlsmith
 5 years ago
Best ResponseYou've already chosen the best response.0https://gist.github.com/944461 Try reading through this, it should help you out no end.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0here'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 ",(candidate2))

carlsmith
 5 years ago
Best ResponseYou've already chosen the best response.0How 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 = = = = = = = = = = = =

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0would 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?

carlsmith
 5 years ago
Best ResponseYou've already chosen the best response.0Ah, 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 notlessthanhalfofnumber (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 :)

carlsmith
 5 years ago
Best ResponseYou've already chosen the best response.0I 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0well, i'll understand it better, by and by... :D
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.