-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprime_palindrome.py2
More file actions
56 lines (49 loc) · 1.13 KB
/
prime_palindrome.py2
File metadata and controls
56 lines (49 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def isprime(num):
"""
int -> bool
checks if a number is prime, only need to check odd numbers (if greater than 2),
and only need to try dividing by numbers less than half the number. All numbers
less than 2 are not prime.
>>> isprime(5)
True
>>> isprime(10)
False
"""
if num == 2:
return True
if num % 2 == 0 or num < 2:
return False
for i in range(3,num//2 + 1,2):
if (num % i == 0):
return False
return True
def ispalindrome(num):
"""
int -> bool
checks if a number is a palindrome, only need to check numbers with more than one
digit.
>>> ispalindrome(123)
False
>>> ispalindrome(121)
True
"""
return num == int(str(num)[::-1])
def prime_palindrome():
"""
() -> int
Write a program to determine the biggest prime palindrome under 1000.
>>> prime_palindrome()
929
"""
# only need to look at odd numbers.
t = True
i = 999
while t:
if isprime(i):
if ispalindrome(i):
return i
i -= 2
print prime_palindrome()
if __name__ == '__main__':
import doctest
doctest.testmod()