Project Euler Problem4
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的更多相关文章
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- 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 ...
- project euler 169
project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...
- 【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 × ...
- Project Euler 第一题效率分析
Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
- Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
随机推荐
- angular2 bootstrap modal
----html------- <div #ele class="modal fade " style="z-index: 9999" data-back ...
- linux下&、nohup与screen的比较
& 首先,linux进程是区分前台进程和后台进程的. 通常,在终端输入的命令执行的前台进程模式.如果一个命令要执行好久,就会阻塞住终端好久,不能进行其他工作,所以,我们可以把执行花费时间很长的 ...
- 1643【例 3】Fibonacci 前 n 项和
1643:[例 3]Fibonacci 前 n 项和 时间限制: 1000 ms 内存限制: 524288 KB sol:这题应该挺水的吧,就像个板子一样 1 0 01 1 0 * ...
- Python day5 --------递归、匿名函数、高阶函数、内置函数
一.递归 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 递归要求: 1. 必须有一个明确的结束条件 2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减 ...
- hdwiki 部署
1.安装wamp 集成环境(部署过程出现的环境问题请搜索我的另外一篇文章 <wamp安装失败原因大全>)2.到 http://kaiyuan.hudong.com/download/ 下载 ...
- 【刷题】BZOJ 1023 [SHOI2008]cactus仙人掌图
Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple cycle)里,我们就称这张图为仙人掌图(cactus).所谓简单回路就是指在图上不重复经过任何一个顶点的 ...
- 洛谷U19464 山村游历(Wander)(LCT)
洛谷题目传送门 LCT维护子树信息常见套路详见我的总结 闲话 题目摘自WC模拟试题(by Philipsweng),原题目名Wander,"山村游历"是自己搞出来的中文名. 数据自 ...
- HGOI20181030 模拟题解
problem:给定一个序列,问你能不能通过一次交换把他弄成有序 sol: 对于0%的数据,满足数列是一个排列,然后我就打了这档分(自己瞎造的!) 对于100%的数据,显然我们先对数列进行排序然后上下 ...
- A guess 解题报告
A guess 题意 选一个\([1,n](n\le 500)\)的整数,可以询问数是否属于区间\([l,r]\),多次询问一起回答,统计有多少种询问区间集合(无序)满足可以猜出这个数,对\(p(2^ ...
- LeetCode 6罗马数字转整数
罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列 ...