xxxxxxxxxx
def prime_number(n):
c = 0
for x in range(2, n):
if n % x == 0:
c = c + 1
return c
n = int(input("Enter a number = "))
if prime_number(n) == 0:
print("Prime number.")
else:
print("Not prime number.")
xxxxxxxxxx
from math import sqrt
for i in range(2, int(sqrt(num)) + 1):
if num % i == 0:
print("Not Prime")
break
print("Prime")
# Note: Use this if your num is big (ex. 10000 or bigger) for efficiency
# The result is still the same if the num is smaller
xxxxxxxxxx
def is_prime(n):
if n == 0 or n == 1 or n == 4:
return False
for i in range(2, int(n/2)):
if n % i == 0:
return False
return True
A relatively "faster" prime check.
It gets noticably slow once it gets to 8+ digits.
xxxxxxxxxx
a=int(input('print number:'))
for i in range(2,a):
if a%i !=0:
continue
else:
print("Its not a prime number")
break # here break is exicuted then it means else would not be exicuted.
else:
print("Its a prime number")
xxxxxxxxxx
def prime_number(a):
for i in range(2,a):
if a%i == 0:
return False
return True
n = int(input("Enter a number = "))
print(prime_number(n))
xxxxxxxxxx
# Python function to check if a number prime or not.
print("Welcome to Prime Number Checker")
def prime_check(number):
prime = True
for num in range(2, number):
if number % num == 0:
prime = False
if prime:
print("It is prime")
else:
print("It is not prime")
number_to_check = int(input("Type the number to check: "))
prime_check(number = number_to_check)
xxxxxxxxxx
def prime_checker(number):
is_prime = True #use a bool to flag prime number
for i in range(2, number):# starts at 2 and loops until the range of number
if number % i == 0:# if is divisible not a prime
is_prime = False
if is_prime == True:
print(f"{number} is a prime number")
else:
print(f"{number} is a not a prime number.")
n = int(input("Check this number: "))#check a number for prime
prime_checker(number=n)#call the function
xxxxxxxxxx
#prime number verification program
a=int(input('print number:'))
for i in range(2,a):
if a%i !=0:
continue
else:
print("Its not a prime number")
break # here break is exicuted then it means else would not be exicuted.
else:
print("Its a prime number")#this is out of the for loop suite.
xxxxxxxxxx
import math
a=[i for i in range(2,int(input('prime number range'))) if 0 not in [i%n for n in range(2,int(math.sqrt(i)))]]
print(a)
xxxxxxxxxx
# This shows how we can use for + else using a break in between
for x in range(1,101):
# if you want to find whether a user input is a prime number
# use the following insted of the first for loop
# x = int(input("Type a number: "))
for i in range(2, x):
if x % i == 0:
print(x, "is not a prime number.")
break
else:
print(x, "is a prime number.")
# This will print all the numbers from 1-100,
# in the same line will print whether it is a prime or not
# if you use the user input method
# when you type 9, the output will be:
# 9 is not a prime number.
# when you type 7, the output will be:
# 7 is a prime number.