At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
So I have this code so far... count=1 number=3 divisor=2 while(count<1000): (while (divisor
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
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