Project Euler 4

Project Euler 4

题目

Largest palindrome product

A palindromic number reads the same both ways. The largest palindrome made from the product of two \(2\)-digit numbers is \(9009 = 91 \times 99\).

Find the largest palindrome made from the product of two \(3\)-digit numbers.

解决方法

直接枚举所有的三位数和三位数的乘积,然后再判断乘积的值是否为回文字符串即可。

代码

1
2
3
4
5
6
7
8
N = 3
ans = 0
for i in range(10**(N-1), 10**N):
for j in range(10**(N-1), 10**N):
s = i * j
if str(s) == str(s)[::-1]:
ans = max(ans, s)
print(ans)
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
Ujimatsu Chiya 微信 微信
Ujimatsu Chiya 支付宝 支付宝