Python program to find Prime Numbers between 1 to100.
Find Prime number between 1 to100
Here, in this page we will discuss program to find Prime number between 1 to100 in python .A prime number is an positive integer that has no divisors except one and itself or can only be exactly divided by the integers 1 and itself without leaving a remainder.
Methods Discussed in page
- Method 1 :Basic checking prime by only checking first n
- Method 2: Basic checking prime by only checking first n/2 divisors
- Method 3: Checking prime by only checking first √n divisors
- Method 4: Checking prime by only checking first √n divisors, but also skipping even iterations.
Method used to check prime
Here we use the usual method to check prime. If given number is prime then we print it else we move ahead to the next number and check if that is prime and keep going till 100
Method 1
Python Code
Run
def checkPrime(num): # 0, 1 and negative numbers are not prime if num < 2: return 0 else: # no need to run loop till num-1 as for any number x the numbers in # the range(num/2 + 1, num) won't be divisible anyway # Example 36 won't be divisible by anything b/w 19-35 x = num // 2 for j in range(2, x + 1): if num % j == 0: return 0 # the number would be prime if we reach here return 1 a, b = 1, 100 for i in range(a, b + 1): if checkPrime(i): print(i, end=" ")
Output
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
Method 2
Algorithm (Method 2)
- Run a loop in the iteration of (i) b/w 1 and 100 bounds.
- For each, i check if its prime or not using function checkPrime(i)
- If i is prime print it else move to the next iteration
Method used to check prime
Here we use the usual method to check prime. We however check the divisibility only till num/2.
As the range(num/2 + 1, num) won't be divisible anyways. Example 36 won't be divisible by anything b/w 19-35
As the range(num/2 + 1, num) won't be divisible anyways. Example 36 won't be divisible by anything b/w 19-35
Python Code
Run
def checkPrime(num): # 0, 1 and negative numbers are not prime if num < 2: return 0 else: # no need to run loop till num-1 as for any number x the numbers in # the range(num/2 + 1, num) won't be divisible anyway # Example 36 won't be divisible by anything b/w 19-35 x = num // 2 for j in range(2, x + 1): if num % j == 0: return 0 # the number would be prime if we reach here return 1 for i in range(1, 100 + 1): if checkPrime(i): print(i, end=" ")
Output
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
Method 3
The outer logic remains the same. Only the method to check prime changes to make code more optimized. Has better time complexity of O(√N)
- Run a loop in the iteration of (i) b/w 1 and 100 bounds.
- For each, i check if its prime or not using function checkPrime(i)
- If i is prime print it else move to next iteration
Method used to check prime
A number n is not a prime if it can be factored into two factors a & b:
n = a * b
Now a and b can't be both greater than the square root of n, since then the product a * b would be greater than sqrt(n) * sqrt(n) = n.
So in any factorization of n, at least one of the factors must be smaller than the square root of n, and if we can't find any factors less than or equal to the square root, n must be a prime.
So we only need to run loop till sqrt(n) and not n or n/2
n = a * b
Now a and b can't be both greater than the square root of n, since then the product a * b would be greater than sqrt(n) * sqrt(n) = n.
So in any factorization of n, at least one of the factors must be smaller than the square root of n, and if we can't find any factors less than or equal to the square root, n must be a prime.
So we only need to run loop till sqrt(n) and not n or n/2
Python Code
Run
from math import sqrt def checkPrime(num): # 0, 1 and negative numbers are not prime if num < 2: return 0 else: # A number n is not a prime, if it can be factored into two factors a & b: # n = a * b """Now a and b can't be both greater than the square root of n, since then the product a * b would be greater than sqrt(n) * sqrt(n) = n. So in any factorization of n, at least one of the factors must be smaller than the square root of n, and if we can't find any factors less than or equal to the square root, n must be a prime.""" for j in range(2, int(sqrt(num))): if num % j == 0: return 0 # the number would be prime if we reach here return 1 a, b = 1, 100 for i in range(a, b + 1): if checkPrime(i): print(i, end=" ")
Output
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
Method 4
The outer logic remains the same. Only the method to check prime changes to make code more optimized. Has same time complexity of O(√N).
But makes around half lesser checks
- Run a loop in the iteration of (i) b/w 1 to 100 bounds.
- For each, i check if its prime or not using function checkPrime(i)
- If i is prime print it else move to next iteration
Method used to check prime
This method uses the assumption that –
We can simply check all divisors between [2, √num]
2 is the only even prime number
We can skip all even iterations in the loop
We can simply check all divisors between [2, √num]
2 is the only even prime number
We can skip all even iterations in the loop
Python Code
Run
from math import sqrt def checkPrime(n): # 0 and 1 are not prime numbers # negative numbers are not prime if n <= 1: return 0 # special case as 2 is the only even number that is prime elif n == 2: return 1 # Check if n is a multiple of 2 thus all these won't be prime elif n % 2 == 0: return 0 # If not, then just check the odds for i in range(3, int(sqrt(n)), 2): if n % i == 0: return 0 return 1 a, b = 1, 100 for i in range(a, b + 1): if checkPrime(i): print(i, end=" ") # This method is obviously faster as makes around half lesser comparison due skipping even iterations in the loop
Output
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
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Getting Started
- ASCII Table
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
n=int(input())
for i in range(1,n):
fact=0
for j in range(2,i):
if i%j==0:
fact+=1
if fact==0:
print(i)
for i in range(1,100):
c=0
for j in range(1,i+1):
if i%j==0:
c=c+1
if c==2:
print(i,end=’ ‘)
More Easy:
for i in range(2, 100):
for j in range(2, i):
if i % j == 0:
break
else:
print(i)
start = int(input())
end = int(input())
for i in range(start, end+1):
if i>1:
for j in range(2,i):
if(i % j==0):
break
else:
print(i)
for i in range(2,101):
if i>1:
for j in range(2,i):
if(i % j==0):
break
else:
print(i)