Write a program of prime number in python - Prime Number program in python - Python Programs - Pattern programs in python

Prime Number Program In Python



Here's a Python program to check if a given number is prime or not:

Python Code:

def is_prime(number):

    """

    Returns True if the given number is prime, False otherwise.

    """

    if number < 2:

        return False

    for i in range(2, int(number ** 0.5) + 1):

        if number % i == 0:

            return False

    return True

Here is the Practical of the above program in Jupyter Notebook



# example usage

print(is_prime(7))   # True

print(is_prime(10))  # False

print(is_prime(23))  # True

The is_prime function takes a number as input and returns True if the number is prime, and False otherwise. The function first checks if the number is less than 2, because 1 and 0 are not prime numbers. The function then iterates from 2 up to the square root of the number, and checks if the number is divisible by any of the numbers in this range. If the number is divisible by any of these numbers, the function returns False. If the number is not divisible by any of these numbers, the function returns True.

Note that this program is not the most efficient algorithm to check for prime numbers, especially for very large numbers. There are more sophisticated algorithms, such as the Sieve of Eratosthenes or the Miller-Rabin primality test, that are much faster for large numbers.

Comments