Largest palindrome product

Problem 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.

The python code is as follows:

def isPalindromic(data):
list = []
while data > 10:
list.append(data%10)
data = int(data/10)
list.append(data)
print(list)
i = 0
j = len(list)-1
while i < j:
if list[i] != list[j]:
return False
i += 1
j -= 1
return True a = 999
total = 0
result = 0
targetA = 0
targetB = 0
while a >= 100:
b = 999
if a*b < result:
break
while b >= 100:
total = a*b
if total < result:
break
if isPalindromic(total):
result = total
targetA = a
targetB = b
b -= 1
a -= 1 print(result)
print(targetA)
print(targetB)

  Assuming b is large than a, we can rewrite the code like this:

def isPalindromic(data):
list = []
while data > 10:
list.append(data%10)
data = int(data/10)
list.append(data)
print(list)
i = 0
j = len(list)-1
while i < j:
if list[i] != list[j]:
return False
i += 1
j -= 1
return True a = 999
total = 0
result = 0
targetA = 0
targetB = 0
while a >= 100:
b = 999
if a*b < result:
break
while b >= a:
total = a*b
if total < result:
break
if isPalindromic(total):
result = total
targetA = a
targetB = b
b -= 1
a -= 1 print(result)
print(targetA)
print(targetB)

  

Project Euler Problem4的更多相关文章

  1. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  2. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  3. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  4. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

  5. project euler 169

    project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...

  6. 【Project Euler 8】Largest product in a series

    题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...

  7. Project Euler 第一题效率分析

    Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...

  8. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  9. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

随机推荐

  1. 更新pip10后 ImportError: cannot import name ‘main'

    百度了几个回答都没有解决问题,有些回答明显是直接复制过来的一点价值都没有,然后google一下立马解决.很多时候不能怪搜索引擎,问题出在一些国内网友对知识的不负责任 解决:找到报错文件,也就是那个pi ...

  2. ElasticSearch 2 (15) - 深入搜索系列之多字段搜索

    ElasticSearch 2 (15) - 深入搜索系列之多字段搜索 摘要 查询很少是简单的一句话匹配(one-clause match)查询.很多时候,我们需要用相同或不同的字符串查询1个或多个字 ...

  3. Beta冲刺——day3

    Beta冲刺--day3 作业链接 Beta冲刺随笔集 github地址 团队成员 031602636 许舒玲(队长) 031602237 吴杰婷 031602220 雷博浩 031602134 王龙 ...

  4. Xshell登录Docker

    使用SSH协议登录即可,用户名为docker,密码为tcuser

  5. 查看django版本的方法

    在cmd输入: python -m django --version django-admin --version

  6. dx、aapt工具

    1.DX命令: Dalvik虚拟机不能直接运行Java代码编译出来的文件,只能运行.dex文件,所以需要将.class,.jar等文件通过DX工具转换成.dex文件. 2.AAPT命令 一个Andro ...

  7. js 复制到剪切板

    function copyTextToClipboard(text) { var copyFrom = $('<textarea/>'); copyFrom.text(text); $(' ...

  8. delphi制作登陆窗体

    delphi登陆窗体的制作,就我知道的,可以有两种方法,一种是在工程文件中实现登陆窗体的动态调用,另一种就是在主窗体的OnCreate事件中动态创建登陆窗体,两种方法都需要将主窗体设置为Auto-cr ...

  9. Redis4.0新特性之-大KEY删除

    接上一篇,我们得知了redis中存在大KEY,那么这个大KEY如何删除呢?本文将从源码角度分析Redis4.0带来的新特性. 在Redis中,对于大KEY的删除一直是个比较头疼的问题,为了不影响服务, ...

  10. Luogu4980 【模板】Polya定理(Polya定理+欧拉函数)

    对于置换0→i,1→i+1……,其中包含0的循环的元素个数显然是n/gcd(i,n),由对称性,循环节个数即为gcd(i,n). 那么要求的即为Σngcd(i,n)/n(i=0~n-1,也即1~n). ...