[Tutor] noob
Andre Engels
andreengels at gmail.com
Mon Nov 29 12:05:50 CET 2010
On Mon, Nov 29, 2010 at 6:57 AM, Mary <sharky5000 at comcast.net> wrote:
> Dear Tutors:
>
> Thank you for your time.I am trying to do first assignment (ps1a+b) onMIT
> open study, finding the 1000th prime in part a and doing something with
> import.math and logs in part b, but I'm not there yet. The little build i
> did to find primes does fine until, for some reason, 95 shows up, and later
> other multiples of 5 jump into the mix without being invited.I end up almost
> 200 primes off by the 1000 count. Is it me? Have uninstalled and reinstalled
> 2.7 twice. Here is code:
Your primality tester is incorrect. When you find a divisor you go on
to check the number that is 2 more, but you start with the same
divisor. If the new number you check is not a prime number, but only
has divisors smaller than the checked divisor, your program will
falsely recognize it as prime.
To make it more clear what I mean, I will show how your program finds
95 to be prime:
89 was a prime, and next the program checks 91.
91 is not divisible by 2.
91 is not divisible by 3.
91 is not divisible by 4.
91 is not divisible by 5.
91 is not divisible by 6.
91 is divisible by 7 - not prime! check 93 instead
93 is not divisible by 7.
93 is not divisible by 8.
...
93 is not divisible by 30.
93 is divisible by 31 - not prime! check 95 instead
95 is not divisible by 31.
...
95 is not divisible by 90.
Checked all numbers smaller than 91, so 95 is prime.
--
André Engels, andreengels at gmail.com
More information about the Tutor
mailing list