This Python program reads an integer n from the user.
It prints all prime numbers less than n using the following definition:
A number is prime if it is not divisible by any prime number before it.
This recursive approach builds the list of primes step by step.
n = int(input("Enter an integer: "))
prime_list = []
for num in range(2, n):
is_prime = True
for p in prime_list:
if num % p == 0:
is_prime = False
break
if is_prime:
prime_list.append(num)
print("Prime numbers less than", n, "are:")
print(prime_list)
Enter an integer: 20
Prime numbers less than 20 are:
[2, 3, 5, 7, 11, 13, 17, 19]
Here’s how the program works:
- It starts with an empty list of primes
- For each number from 2 to n - 1, it checks divisibility only against known primes
- If the number is not divisible by any previous prime, it is added to the list
- The final list is printed using print()