首页 > 代码库 > 欧拉项目004:寻找最大的回文数
欧拉项目004:寻找最大的回文数
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.
寻找有两个三位数相乘的最大回文数
我的python代码如下:
#method1: def reverse(s): return s[::-1] maxPalindromic = 0 for i in range(100,999): for j in range(i,999): s = str(i*j) if s == reverse(s): if int(s)>maxPalindromic: maxPalindromic = int(s) print maxPalindromic #method2: def reverse(s): return s[::-1] def canBeDivide(num): for i in range(100,1000): if num%i==0 and num/i<1000: return True return False def findMaxPalindromic(): for x in range(9,0,-1): for y in range(9,-1,-1): for z in range(9,-1,-1): num = 100001*x+10010*y+1100*z if canBeDivide(num): return num print findMaxPalindromic()方法一,比较直观,容易理解;方法二,效率高。
有不懂得或者有更好的想法,可以留言,互相学习啊!
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。