You can print prime numbers from 1 to a specified value n
in Python using a function that checks for primality and iterates through the range, and then it returns the list of prime numbers for that range.
Concept of Prime Number
The concept of a prime number is a fundamental concept in number theory. A prime number is a positive integer greater than 1 that has exactly two distinct positive divisors: 1 and itself.
Here are the key points of the concept:
Definition: A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers.
Examples: Some examples of prime numbers include 2, 3, 5, 7, 11, 13, 17, and so on. These numbers are only divisible by 1 and themselves.
Python Program to Print prime numbers from 1 to n
In Python, writing a program to print prime numbers from 1 to n involves creating a function that checks if a number is prime and then iterating through the numbers in the desired range, adding prime numbers to a list, and finally displaying or returning the list of prime numbers. This program conceptually identifies and lists numbers that are divisible by only 1 and themselves, making them prime.
Algorithm
- Start
- Create a function
is_prime(num)
that takes a numbernum
as input and returnsTrue
ifnum
is prime andFalse
otherwise. - Initialize an empty list
prime_numbers
to store the prime numbers found. - Iterate through numbers from 1 to
n
using afor
loop. - For each number
i
, check if it is prime using theis_prime
function. - If
i
is prime, append it to theprime_numbers
list. - Finally, return the
prime_numbers
list containing all prime numbers from 1 ton
. - End
Source Code
You can print prime numbers from I to n using the following source code
def is_prime(num): if num <= 1: return False if num <= 3: return True if num % 2 == 0 or num % 3 == 0: return False i = 5 while i * i <= num: if num % i == 0 or num % (i + 2) == 0: return False i += 6 return True def print_primes_up_to_n(n): prime_numbers = [] for i in range(1, n + 1): if is_prime(i): prime_numbers.append(i) return prime_numbers n = 20 # Change this value to the desired upper limit prime_numbers = print_primes_up_to_n(n) print("Prime numbers from 1 to", n, "are:", prime_numbers)
Steps to write code
Part-1: Define is_prime(num) Function
def is_prime(num):
Step-1: Check if num
is less than or equal to 1. If it is, return False
,
if num <= 1: return False
Step2: Check if num
is less than or equal to 3. If it is, return True
if num <= 3: return True
Step-3: Check if num
is divisible by 2 or 3. If it is, return False
, as even numbers greater than 2 and numbers divisible by 3 are not prime.
if num % 2 == 0 or num % 3 == 0: return False
Step-4: Initialize i
to 5.
i = 5
Step-5: Enter a while loop that continues while the square of i
is less than or equal to num
.
while i * i <= num:
Step-6: Check if num
is divisible by i
or i + 2
. If either condition is met, return False
, as the number is not prime.
if num % i == 0 or num % (i + 2) == 0: return False
Step-7: Increment i
by 6 (this is an optimization, as we are only checking numbers that are 1 or 5 more than a multiple of 6).
i += 6
Step-8: If none of the conditions above were met in the loop, return True
because num
is prime.
return True
Part-2: Define print_primes_up_to_n(n) Function:
def print_primes_up_to_n(n):
Step-1: Create an empty list called prime_numbers
to store the prime numbers found.
prime_numbers = []
Step-2: Iterate through numbers from 1 to n
(inclusive) using a for
loop.
for i in range(1, n + 1):
Step-3: For each number i
in the range, call the is_prime
function to check if i
is prime.
if is_prime(i):
Step-4: If is_prime(i)
returns True
, append i
to the prime_numbers
list.
prime_numbers.append(i)
Step-5: After the loop, return the prime_numbers
list containing all prime numbers up to n
.
return prime_numbers
Part-3: Main Part of the Code
Step-1: Set the value of n
to 20 (you can change this value to set the upper limit for finding prime numbers).
n = 20
Step-2: Call the print_primes_up_to_n
function with the value of n
to find all prime numbers up to n
.
prime_numbers = print_primes_up_to_n(n)
Step-3: Print the prime numbers found along with a message.
print("Prime numbers from 1 to", n, "are:", prime_numbers)
Output
When you pass n = 20 then it will print all prime numbers from 1 to the specified value 20 of n
. You can change the value of n
to find prime numbers in a different range.
n = 20 # Change this value to the desired upper limit prime_numbers = print_primes_up_to_n(n) print("Prime numbers from 1 to", n, "are:", prime_numbers)
output
Prime numbers from 1 to 20 are: [2, 3, 5, 7, 11, 13, 17, 19]