python中3位数的最高回文数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12674389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:34:31  来源:igfitidea点击:

highest palindrome with 3 digit numbers in python

pythonmathlogicpalindrome

提问by FabianCook

In problem 4 from http://projecteuler.net/it says:

http://projecteuler.net/ 的问题 4 中,它说:

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.

回文数的读法是一样的。由两个两位数的乘积构成的最大回文数是 9009 = 91 * 99。

找出由两个 3 位数字的乘积构成的最大回文数。

I have this code here

我这里有这个代码

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)

It should find the largest palindrome, it spits out 580085which I believe to be correct, but project euler doesn't think so, do I have something wrong here?

它应该找到最大的回文,它吐出580085我认为是正确的,但项目 euler 不这么认为,我这里有什么问题吗?



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

当我敬畏 for 循环时,我没有考虑清楚,我删除了检查最大、最愚蠢的我的东西。这是工作代码

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)

it spits out 906609

它吐出 906609

采纳答案by John Kugelman

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 xbut a larger y.

反向迭代不会找到最大的x*y,它会找到最大的回文x。有一个比 580085 更大的答案;它有一个较小x但较大的y.

回答by Jon Clements

This would more efficiently be written as:

这将更有效地写为:

from itertools import product

def is_palindrome(num):
    return str(num) == str(num)[::-1]

multiples = ( (a, b) for a, b in product(xrange(100,999), repeat=2) if is_palindrome(a*b) )
print max(multiples, key=lambda (a,b): a*b)
# (913, 993)

You'll find itertoolsand generators very useful if you're doing Euler in Python.

itertools如果您在 Python 中执行 Euler,您会发现生成器非常有用。

回答by blueberryfields

Tried making it more efficient, while keeping it legible:

尝试使其更高效,同时保持清晰:

def is_palindrome(num):
    return str(num) == str(num)[::-1]

def fn(n):
    max_palindrome = 1
    for x in range(n,1,-1):
        for y in range(n,x-1,-1):
            if is_palindrome(x*y) and x*y > max_palindrome:
                max_palindrome = x*y
            elif x * y < max_palindrome:
                break
    return max_palindrome

print fn(999)

回答by Rudney

ReThink: efficiency and performance

重新思考:效率和性能

def palindrome(n):    

    maxNumberWithNDigits = int('9' * n) #find the max number with n digits

    product = maxNumberWithNDigits * maxNumberWithNDigits 

    #Since we are looking the max, stop on the first match

    while True:        
        if str(product) == str(product)[::-1]: break;

        product-=1

    return product

start=time.time()
palindrome(3)
end=time.time()-start

palindrome...: 997799, 0.000138998031616 secs

回文...:997799, 0.000138998031616 秒

回答by user3833942

Not the most efficient answer but I do like that it's compact enough to fit on one line.

不是最有效的答案,但我确实喜欢它足够紧凑,可以放在一条线上。

print max(i*j for i in xrange(1,1000) for j in xrange(1,1000) if str(i*j) == str(i*j)[::-1])

回答by CooLee

Here I added two 'break' to improve the speed of this program.

这里我添加了两个'break'来提高这个程序的速度。

def is_palindrome(num):
    return str(num) == str(num)[::-1]
def max_palindrome(n):
    max_palindrome = 1
    for i in range(10**n-1,10**(n-1)-1,-1):
        for j in range(10**n-1,i-1,-1):
            if is_palindrome(i*j) and i*j > max_palindrome:
                max_palindrome = i * j
                break
            elif i*j < max_palindrome:
                break
    return max_palindrome
n=int(raw_input())
print max_palindrome(n)

回答by kaffuffle

Simple:

简单的:

def is_pallindrome(n):
    s = str(n)
    for n in xrange(1, len(s)/2 + 1):
        if s[n-1] != s[-n]:
            return False    
    return True

largest = 0
for j in xrange(100, 1000):
    for k in xrange(j, 1000):
        if is_pallindrome(j*k):
            if (j*k) > largest: largest = j*k
print largest

回答by Anoop M

Each time it doesnot have to start from 999 as it is already found earlier.Below is a simple method using string function to find largest palindrome using three digit number

每次都不必从 999 开始,因为它之前已经找到了。 下面是一个使用字符串函数使用三位数字查找最大回文的简单方法

def palindrome(y):
    z=str(y)
    w=z[::-1]
    if (w==z):
        return 0
    elif (w!=z):
        return 1        
h=[]
a=999
for i in range (999,0,-1):
    for j in range (a,0,-1):
    l=palindrome(i*j)
    if (l==0):
        h=h+[i*j]               
    a-=1
print h
max=h[0]

for i in range(0,len(h)):
    if (h[i] > max):
    max= h[i]
print "largest palindrome using multiple of three digit number=%d"%max  

回答by Darshi

Here is my code to solve this problem.

这是我解决这个问题的代码。

lst = []
for i in range(100,1000): 
    for n in range(2,i) : 
        lst.append (i* n) 
        lst.append(i*i)

lst2=[]
for i in lst: 
    if str(i) == str(i)[::-1]:
        lst2.append(i)
print max(lst2) 

回答by CSEKU_150236

580085 = 995 X 583, where 906609 = 993 X 913 found it only by applying brute forcing from top to bottom!

580085 = 995 X 583,其中 906609 = 993 X 913 只能通过从上到下应用蛮力找到它!