Advertisement
brilliant_moves

PalindromicPrimes2.py

May 7th, 2015
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. #PalindromicPrimes2.py
  2. #Uses iteration for greater range of numbers
  3. #Python2.7
  4. #By Chris Clarke
  5. #Version: 2 uses square root
  6. #08.05.2015
  7.  
  8. import math
  9.  
  10. def isPrime(n): # iterative not recursive, range > 100000 (see also RecursivePalindromicPrimes.py)
  11.     if n<2:
  12.         return False
  13.     root_n = int(math.sqrt(n) + 1) # using square root speeds things up
  14.     for i in range(2, root_n):
  15.         if n%i==0:
  16.             return False
  17.     return True
  18.  
  19.  
  20. def isPalindrome(word):
  21.     return word == word[::-1]
  22.  
  23.  
  24. lower = int(input("Enter lower limit: "))
  25. upper = int(input("Enter upper limit: "))
  26.  
  27. for n in range(lower, upper):
  28.     if isPrime(n):
  29.         if isPalindrome(str(n)):
  30.             print n, "is a palindromic prime"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement