zoukankan      html  css  js  c++  java
  • Python练习题 032:Project Euler 004:最大的回文积

    本题来自 Project Euler 第4题:https://projecteuler.net/problem=4

    # Project Euler: Problem 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 × 99.
    # Find the largest palindrome made from the product of two 3-digit numbers.
    # Answer: 906609
    
    lst = []
    for i in range(100, 999):
        for j in range(100, 999):
            if str(i*j) == str(i*j)[::-1]:
                lst.append(i*j)
    print(max(lst))
    

    所谓的“回文积”(Palindrome Product),指的是某个数字,正着念、倒着念都一样,而且这个数字是另外两个数字之乘积。比如:9009 = 91 × 99,9009就是个回文数,而且是91和99的乘积,暂且就称之为回文积。本题就是求解两个三位数之积中的最大回文数。

    因为此前在 Python练习题 025:判断回文数 里已做过类似的题目,所以也算是轻车熟路了。思路也很简单:用两个 for 循环轮番找出所有“两个三位数之积中的所有回文数”,用 max() 找出最大的那个即可。而判断回文数,最简单的就是用 str() 把数字转化为字符,如果 str == str[::-1],就判断为是回文,因为 str[::-1] 的作用就是把 str 倒着写。

  • 相关阅读:
    21天搞定聊天机器人之{命名实体识别}
    e到底是什么?
    An example of using Pandas for regression
    Time-Series Analysis
    Multiple Regression
    Regression Analysis Using Excel
    Correlation and Regression
    Hypothesis Testing
    Sampling and Estimation
    Common Probability Distributions
  • 原文地址:https://www.cnblogs.com/iderek/p/6008307.html
Copyright © 2011-2022 走看看