Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #Find prime factors in python
  2. from math import sqrt
  3.  
  4. def primality_testing(n):
  5. """test if the given number is prime or not"""
  6. if type(n) != int:
  7. print("the given number is not a positive integer")
  8. elif n == 1:
  9. return False
  10. elif n == 2:
  11. return True
  12. elif n == 3: #exception occurs when n == 3
  13. return True
  14. elif n > 2:
  15. if int(sqrt(n)) > 2:
  16. for i in range(2, int(sqrt(n))):
  17. b = n % i #Check if the modulo of n against a trial division number i is 0
  18. if b == 0:
  19. return False #if modulo is zero, then the given number is a factor, so n is not a prime number
  20. return True #if modulo is not zero, then the given number is not a factor, so n is a prime number
  21. elif int(sqrt(n)) == 2: #taking out the exceptional case when sqrt(n) = 2
  22. b = n % 2 #the trial division number here is 2
  23. if b == 0:
  24. return False
  25. return True
  26.  
  27. is_prime = primality_testing(13)
  28. print(is_prime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement