Prime Numbers Less Than N
Aim
Write a program to print all prime numbers less than a given number N.
Algorithm
- Start
- Define a function is_primeto check if a number is prime.
- Iterate through all numbers less than N.
- Use the is_primefunction to check if the number is prime.
- Print the prime numbers.
- End
Program
# Function to check if a number is prime
def is_prime(num):
    if num <= 1:
        return False
    for i in range(2, int(num**0.5) + 1):
        if num % i == 0:
            return False
    return True
 
# Function to print all prime numbers less than N
def print_primes_less_than_n(n):
    for num in range(2, n):
        if is_prime(num):
            print(num, end=' ')
 
# Take input from the user
n = int(input("Enter a number: "))
print(f"Prime numbers less than {n} are:")
print_primes_less_than_n(n)Sample Input/Output
Enter a number: 20
Prime numbers less than 20 are:
2 3 5 7 11 13 17 19Explanation
- 
The program defines a function is_primeto check if a number is prime:- The function takes a number numas input.
- If numis less than or equal to 1, it returnsFalsebecause 0 and 1 are not prime numbers.
- For each number ifrom 2 to the integer part of the square root ofnum:- If numis divisible byi(i.e.,num % i == 0), it returnsFalsebecausenumhas a divisor other than 1 and itself.
 
- If 
- If no divisors are found, it returns Truebecausenumis a prime number.
 
- The function takes a number 
- 
The program defines a function print_primes_less_than_nto print all prime numbers less than N:- The function takes a number nas input.
- For each number numfrom 2 ton-1:- It uses the is_primefunction to check ifnumis prime.
- If numis prime, it printsnumfollowed by a space.
 
- It uses the 
 
- The function takes a number 
- 
The program takes an input number Nfrom the user:- It asks the user to enter a number and stores it in the variable n.
 
- It asks the user to enter a number and stores it in the variable 
- 
The program prints all prime numbers less than Nusing theprint_primes_less_than_nfunction:- It calls the print_primes_less_than_nfunction withnas the argument.
- The function prints all prime numbers less than n.
 
- It calls the 
- 
Example: - If the user enters 20, the program will print2 3 5 7 11 13 17 19because these are the prime numbers less than 20.
 
- If the user enters 
Complexity Analysis
The time complexity of the is_prime function is O(√n) as it checks divisibility up to the square root of the number. The overall time complexity of the program is O(n√n) as it checks all numbers less than N.
The space complexity of the program is O(1) as it uses a fixed amount of space irrespective of the input size.
How can you improve this program?
- You can optimize the is_primefunction by skipping even numbers after checking for 2.
- You can use the Sieve of Eratosthenes algorithm to find all prime numbers less than N more efficiently.
Summary
In this tutorial, you learned how to write a Python program to print all prime numbers less than a given number N. You learned how to define a function to check if a number is prime and how to use this function to print prime numbers.