-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprob0004.py
More file actions
48 lines (37 loc) · 1.37 KB
/
Copy pathprob0004.py
File metadata and controls
48 lines (37 loc) · 1.37 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
"""Project Euler Problem 4. https://projecteuler.net/problem=4"""
def palindrome_3d():
"""
produces a set of all 3-digit palindromic numbers
Note: unused in solution; as problem misread at first
(If the factors must also be palindromes, answer is 666,666)
:return: set of 90 3-digit integers
"""
p3d = set()
for i in range(100, 1000):
if i // 100 == i % 10:
p3d.add(i)
return p3d
def palindrome_6d_gen():
"""
generates the next 6-digit palindromic number, starting with the highest product of two 3-digit
numbers (998,001), down to the lowest (100,001).
Note problem does not exclude lower solutions, but one is found in this range
:yield: next 6-digit palindromic number
"""
for i in range(998001, 100000, -1):
# turn number into a string
s = str(i)
if s[0] == s[5] and s[1] == s[4] and s[3] == s[2]:
yield i
def check_for_factors():
"""
checks each 6-digit palindromic number for 3-digit factors
:return: highest 6-digit palindrome equal to product of two 3-digit palindromes
"""
factors = palindrome_3d()
for i in palindrome_6d_gen():
for j in range(100, 1000):
if i % j == 0 and int(i/j) < 1000:
return i, j
solution = check_for_factors()
print(solution)