[Solved] Why does prime number checker say 9 is a prime number?


To check if number is prime you have to validate is it devisible by any number in range [2, sqrt(n)].

Following code does exactly the same:

import math

def is_prime(n):
    for i in range(2, int(math.sqrt(n))+1):
        if n % i == 0:
            return False
    return True

This method is good for small number, but if n is real big number then you need something faster. And in this case you can use Miller–Rabin primality test which checks primality with a certain probability.

5

solved Why does prime number checker say 9 is a prime number?