Help on p1?
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?
Nevermind! Got it!
what was it ? share the code please !
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
https://gist.github.com/944461 Try reading through this, it should help you out no end.
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))
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 = = = = = = = = = = = =
looking good, :D
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?
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 :)
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.
well, i'll understand it better, by and by... :D
Join our real-time social learning platform and learn together with your friends!