简体   繁体   English

从python中3个数字的乘积中找到最大的回文

[英]find the largest palindrome from the product of 3 numbers in python

#Find the largest palindrome made from the product of two 3-digit numbers.

from sys import exit
rev=0
print ' let us start'
for i in range(999,100,-1):
    for j in range(999,100,-1):
        product = i*j
        temp = product 
        rev1 = str(product)[::-1]
        a = rev1
        if temp == a:

            print ' is a palindrome'
            if a > rev:
            rev = a
            c = temp
            h = i
            y = j

print '%r*%r=%r,which is the highest palindrome %r' % ( h, y, c, rev)
print a         
print rev1
print temp
print 'over'

output:i am using sublime text2 as editor 输出:我正在使用sublime text2作为编辑器

let us start
Traceback (most recent call last):
      File "palindrome.py", line 19, in <module>
        print '%r*%r=%r,which is the hghest palindrome %r' % ( h, y, c, l)
    NameError: name 'h' is not defined

n == a will never be True , because k = str(t)[::-1] (namely a ) is a string, while t = i*j namely n is an integer. n == a永远不会是True ,因为k = str(t)[::-1] (即a )是一个字符串,而t = i*jn是一个整数。 Try: 尝试:

a = int(k)

The question is already answered though, here is a bit optimized: 虽然问题已经得到回答,但是这里有些优化:

>>> largest, loopmax = -1, -1
>>> largest_t = (0,0)
>>> for i in range(999,100,-1):
        for j in range(999,100,-1):
            p = i*j
            n = str(p)
            if n == n[::-1]:
                loopmax = p
                break # no need to loop further as j decreases and can never be any greater

        if loopmax > largest:
            largest = loopmax
            largest_t = (i,j)

        if i<largest_t[1]: 
            break # further looping will generate only lower palindromes.


>>> largest
906609
>>> largest_t
(993, 913)

There are less than a million products of two 3-digits numbers therefore the brute-force approach works, to find the largest (numerically) product that is also a palindrome: 少于100万个具有两个3位数数字的产品,因此采用蛮力方法可以找到最大的(按数字表示)也是回文的产品:

all3digit = range(100, 1000) # all 3-digit numbers
products = (x*y for x in all3digit for y in all3digit)

def ispalindrome(p):
    s = str(p)
    return s == s[::-1]

print(max(filter(ispalindrome, products))) # -> 906609

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找出由两个 2 位数字的乘积构成的最大回文 - To find the largest palindrome made from the product of two 2-digit numbers 最大的回文产品python - Largest palindrome product python 找到最大回文数的最快算法,它是具有相同位数的 2 个数字的乘积 - Fastest algorithm to find the largest palindrome that is the product of 2 numbers with the same number of digits 查找由两个3位数组成的乘积所产生的最大回文 - Find the largest palindrome made from the product of two 3-digit numbers, just a small edit needed 使用 numpy 找到由两个 3 位数的乘积组成的最大回文 - Find the largest palindrome made from the product of two 3-digit numbers using numpy 在Python中找到两个三位数乘积的最大回文 - Finding the largest palindrome of the product of two 3-digit numbers in Python 最大回文集,是两个n位数字的乘积(Python) - Largest palindrome which is product of two n-digit numbers (Python) 如何在python中找到由两个三位数组成的最大回文? - How to find the largest palindrome made of two three digit numbers in python? 带有 python 的 Euler 4 项目:最大的回文产品 - Project Euler 4 with python : Largest Palindrome Product 在我的计算中,要从两个3位数的乘积中得到最大的回文数,在哪里出错? - where am I going going wrong in my calculations to get largest palindrome made from the product of two 3-digit numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM