python中具有3位数字的最高回文 [英] highest palindrome with 3 digit numbers in python
问题描述
问题4来自 http://projecteuler.net/,它说:
回文数在两个方向上都相同.由两个两位数的乘积组成的最大回文率是9009 = 91 * 99.
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99.
找出由两个3位数组成的乘积所产生的最大回文.
Find the largest palindrome made from the product of two 3-digit numbers.
我在这里有此代码
def isPalindrome(num):
return str(num) == str(num)[::-1]
def largest(bot, top):
for x in range(top, bot, -1):
for y in range(top,bot, -1):
if isPalindrome(x*y):
return x*y
print largest(100,999)
它应该找到最大的回文,它吐出580085
,我认为是正确的,但是欧拉项目并不这么认为,我在这里有问题吗?
It should find the largest palindrome, it spits out 580085
which I believe to be correct, but project euler doesn't think so, do I have something wrong here?
当我崇敬for循环时,我没有想到,我删除了检查最大的东西的方法,这使我很愚蠢.这是工作代码
When I revered the for loop I didn't think it through, I removed the thing that checks for the biggest, silly me. Heres the working code
def isPalindrome(num):
return str(num) == str(num)[::-1]
def largest(bot, top):
z = 0
for x in range(top, bot, -1):
for y in range(top,bot, -1):
if isPalindrome(x*y):
if x*y > z:
z = x*y
return z
print largest(100,999)
它吐出906609
推荐答案
反向迭代找不到最大的x*y
,而是找到最大的x
回文.答案比580085大.它的x
较小,但y
较大.
Iterating in reverse doesn't find the largest x*y
, it finds the palindrome with the largest x
. There's a larger answer than 580085; it has a smaller x
but a larger y
.
这篇关于python中具有3位数字的最高回文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!