-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.py
49 lines (36 loc) · 1.27 KB
/
4.py
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
#My solution is not the best. Check http://projecteuler.net/thread=4 for more better solutions.
max_3_digit_number = 999
min_3_digit_number = 100
candidates = []
# I use is_palindrome() to verify the number.
# But someone has a very simple way:
# if int(str(a*b)[::-1]) == a*b
def is_palindrome(x):
a = list(str(x))
b = list(str(x))
b.reverse()
if ''.join(a) == ''.join(b):
return True
else:
return False
def is_two_3_digit_number_product(x):
for i in range(min_3_digit_number, max_3_digit_number):
if (x % i) == 0:
if (len(str(i)) == 3) and (len(str(x/i)) == 3):
return True
else:
continue
else:
continue
return False
for i in range(min_3_digit_number*min_3_digit_number, max_3_digit_number*max_3_digit_number):
if is_palindrome(i):
if is_two_3_digit_number_product(i):
candidates.append(i)
else:
continue
else:
continue
print max(candidates)
# A good solution: only 1 line
# print max([ x*y for x in range(100,1000) for y in range(100,1000) if str(x*y) == str(x*y)[::-1]])