Python Program to Check a Prime Number.

Given a number num, write a python program to check if the given number is prime or not. If the number is prime then print true else print false.

Prime Numbers are whole numbers that are not divisible by any other whole number other than itself and 1.(e.g. 2, 3, 5, 7, 11, etc) (alert-success)

Example:

Input: num = 11
Output: True

Input: num = 9
Output: False

Approach 1: Check Prime using a for loop.

In this approach, the program uses a for loop to check if the number is divisible by any number other than 1 and itself. If it is divisible by any such number, the flag is_prime is set to False, and the loop is broken.

Python Program:
# take input for the number
num = int(input("Enter a number: "))

# initialize a flag to check if the number is prime
is_prime = True

# use a for loop to check if the number is divisible by any other number
for i in range(2, num):
    if num % i == 0:
        is_prime = False
        break

# print the result
if is_prime:
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")
Output:
Enter a number: 11
11 is a prime number
  • Time Complexity: O(n)
  • Space Complexity: O(1)

Approach 2: Check Prime using the sqrt function.

In this approach, the program uses the sqrt() function from the math module to only check for divisibility up to the square root of the number. This speeds up the program as the number of iterations is reduced.

Python Code:
# import the sqrt function from the math module
from math import sqrt

# take input for the number
num = int(input("Enter a number: "))

# initialize a flag to check if the number is prime
is_prime = True

# use a for loop to check if the number is divisible by any other number up to its square root
for i in range(2, int(sqrt(num)) + 1):
    if num % i == 0:
        is_prime = False
        break

# print the result
if is_prime:
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")
Output:
Enter a number: 7
7 is a prime number
  • Time Complexity: O(sqrt(n))
  • Space Complexity: O(1)

Approach 3: Check Prime using a while loop.

In this approach, the program uses a while loop to check for divisibility in a similar way as the for loop in approach 1.

Python Code:
# take input for the number
num = int(input("Enter a number: "))

# initialize a flag to check if the number is prime
is_prime = True

# initialize the counter to 2
i = 2

# use a while loop to check if the number is divisible by any other number
while i < num:
    if num % i == 0:
        is_prime = False
        break
    i += 1

# print the result
if is_prime:
    print(num, "is a prime number")
else:
    print(num, "is not a prime number")
Output:
Enter a number: 10
10 is not a prime number
  • Time Complexity: O(n)
  • Space Complexity: O(1)

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS