简体   繁体   English

查找由两个3位数组成的乘积所产生的最大回文

[英]Find the largest palindrome made from the product of two 3-digit numbers, just a small edit needed

I wrote the program however I wonder how can I edit and show in the output the two products from which I got the output: 我写了程序,但是我不知道如何编辑和显示输出中的两个产品:

def check_palindrome(s):
     """Checks whether the given string is palindrome"""
     return s == s[::-1]
max_product = 0
for i in range(999, 900, -1):
    for j in range(i, 900, -1):
        product = i * j
        if check_palindrome(str(product)):
            max_product = max(max_product, product)
print(max_product)

In the same way you are updating max_product , you can use two more variables ( a and b ) and keep updating them when necessary (when product is greater than max_product ): 以相同的方式更新max_product ,可以使用另外两个变量( ab ),并在必要时(当product大于max_product时)继续更新它们:

def check_palindrome(s):
     """Checks whether the given string is palindrome"""
     return s == s[::-1]

max_product = a = b = 0

for i in range(999, 900, -1):
    for j in range(i, 900, -1):
        product = i * j
        if check_palindrome(str(product)):
            if product > max_product:     # if greater product
                max_product = product     # update max_product
                a = i                     # update a
                b = j                     # update b

print('%d * %d = %d' % (a, b, max_product))

Also, you can use this for updating, and for shorter code: 另外,您可以将其用于更新和较短的代码:

max_product, a, b = product, i, j

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

相关问题 使用 numpy 找到由两个 3 位数的乘积组成的最大回文 - Find the largest palindrome made from the product of two 3-digit numbers using numpy 找出由两个 2 位数字的乘积构成的最大回文 - To find the largest palindrome made from the product of two 2-digit numbers 来自两个3位数的乘积的回文 - Palindrome from the product of two 3-digit numbers 在我的计算中,要从两个3位数的乘积中得到最大的回文数,在哪里出错? - where am I going going wrong in my calculations to get largest palindrome made from the product of two 3-digit numbers? 在Python中找到两个三位数乘积的最大回文 - Finding the largest palindrome of the product of two 3-digit numbers in Python 找到两个三位数的最大回文乘积:逻辑错误是什么? - Finding the largest palindrome product of two 3-digit numbers: what is the error in logic? 欧拉计划:两个三位数数字的最大回文产品 - Project Euler: Largest palindrome product of two 3-digit number 如何在python中找到由两个三位数组成的最大回文? - How to find the largest palindrome made of two three digit numbers in python? 从python中3个数字的乘积中找到最大的回文 - find the largest palindrome from the product of 3 numbers in python 最大回文集,是两个n位数字的乘积(Python) - Largest palindrome which is product of two n-digit numbers (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM