python program to print prime numbers upto n

10/16/2022
All Articles

#python program to print prime numbers upto n #python #programmer #pythonQuestion #learnPython

python program to print prime numbers upto n

Python program to print prime numbers upto n

Prime number is define as whole number greater than 1 that cannot be exactly divided by any whole number other than itself and 1 (e.g. 2, 3, 5, 7, 11):

Here we need to create variable and  put the value in loop .

The for loop ranges from 2 to the half of the number so 1 and the number itself  are not counted as divisors.

then we can check the  two condition  are follow :

  • The value of  a%i  is equal to zero that means it is divisible by it and it is not a prime number .
  • The next condition that is else which is checking k is variable counts the number of divisors and if the count is lesser or equal to 0, the number is a prime number.

Below is example of prime number using python program

primeVariable=int(input("Enter upper value  to print prime numbers n : "))
for a in range(2,primeVariable +1):
    k=0
    for i in range(2,a//2+1):
        if(a%i==0):
            k=k+1
    if(k<=0):
        print(a)

 

This is out put of python program if you gien n number is equal to 30 .

Enter upper limit: 30
2
3
5
7
11
13
17
19
23
29

 

What are the prime number from 1 to 100?

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.

what is the range of prime numbers?

To find whether a larger number is prime or not, sum all the digits in a number, if the sum is divisible by 3 so it is not a prime number. There is except 2 and 3 in our input, all the other prime numbers can be present in the general form as 6n + 1 or 6n - 1, where n is the natural number.

Conclusion :

Prime numbers are a fundamental concept in mathematics and have important applications in various fields, including cryptography and computer science. Here are some key conclusions and facts about prime numbers.
  •  Mersenne primes are prime numbers that can be expressed as 2^n - 1, where n is also a prime. 
  •  Twin prime pairs are prime numbers that have a difference of 2, like (3, 5), (11, 13), and (17, 19). 
  •  Prime numbers play a crucial role in modern cryptography, particularly in algorithms like RSA.
  •  Prime numbers can sometimes be observed in natural phenomena, such as patterns in sunflowers and the distribution of atoms in crystals.

To print prime numbers up to a given integer n in Python, you can use a loop to check each number in the range for primality. Here's a simple Python program to achieve this

 

Article