Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Find prime factors in python
- from math import sqrt
- def primality_testing(n):
- """test if the given number is prime or not"""
- if type(n) != int:
- print("the given number is not a positive integer")
- elif n == 1:
- return False
- elif n == 2:
- return True
- elif n == 3: #exception occurs when n == 3
- return True
- elif n > 2:
- if int(sqrt(n)) > 2:
- for i in range(2, int(sqrt(n))):
- b = n % i #Check if the modulo of n against a trial division number i is 0
- if b == 0:
- return False #if modulo is zero, then the given number is a factor, so n is not a prime number
- return True #if modulo is not zero, then the given number is not a factor, so n is a prime number
- elif int(sqrt(n)) == 2: #taking out the exceptional case when sqrt(n) = 2
- b = n % 2 #the trial division number here is 2
- if b == 0:
- return False
- return True
- is_prime = primality_testing(13)
- print(is_prime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement