nek-lang/examples/euler4.py
2022-02-11 01:19:45 +01:00

25 lines
553 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# A palindromic number reads the same both ways. The largest palindrome made from the product of
# two 2-digit numbers is 9009 = 91 × 99.
# Find the largest palindrome made from the product of two 3-digit numbers.
#
# Correct Answer: 906609
def reverse(n):
rev = 0
while n:
rev = rev * 10 + n % 10
n //= 10
return rev
res = 0
for i in range(100, 1_000):
for k in range(i, 1_000):
num = i * k
num_rev = reverse(num)
if num == num_rev and num > res:
res = num
print(res)