[Tutor] help with prime number program
Kent Johnson
kent37 at tds.net
Mon Oct 31 14:54:09 CET 2005
Norman Silverstone wrote:
> On Mon, 2005-10-31 at 06:12 -0500, Kent Johnson wrote:
>
>>Norman Silverstone wrote:
>>
>>>I am a beginner so, I hope what I give, makes sense. In it's simplest
>>>form what is wrong with :-
>>>
>>>n = input("Enter a number")
>>>if n % 2 != 0 and n % 3 != 0:
>>> print n, " Is a prime number"
>>>
>>
>>This only gives the correct answer if n < 25. You can't test against a fixed list of divisors, there will always be a non-prime whose divisors are not in your list.
>
>
> Thank you for your comment which, if I understand you correctly, implies
> that prime numbers greater than 25 will not be recognised. Surely, that
> cannot be correct. However, if it is correct, could you please
> demonstrate what you mean for my education.
More precisely, it will give an incorrect answer for any non-prime that is not divisible by 2 or 3, for example 25.
>>> def test(n):
... if n % 2 != 0 and n % 3 != 0:
... print n, 'is prime'
...
>>> test(2)
>>> test(3)
Hmm, not looking so good for 2 and 3 either...
>>> test(4)
>>> test(5)
5 is prime
>>> test(25)
25 is prime
Not so sure about that one either!
Kent
--
http://www.kentsjohnson.com
More information about the Tutor
mailing list