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 ...
随机推荐
- Maven 学习笔记——Maven和Eclipse(2)
前面已经配置好Maven的环境和本地仓库已经准备好了,下面我们通过Eclipse创建Maven项目. 1.安装Maven集成于Eclipse IDE (Eclipse的版本中如果已经集成了Maven插 ...
- C语言入门:01.C语言概述
一.计算机和软件常识 1.计算机运行原理 (1)硬件基本组成:硬盘.内存.CPU (2)个部件之间的运作协调(下图)
- git learn
$ git config --global user.name "Your Name" $ git config --global user.email "email@e ...
- ThreadLocal变量
什么是ThreadLocal变量?ThreadLocal,很多地方叫做线程本地变量,也有些地方叫做线程本地存储,其实意思差不多.可能很多朋友都知道ThreadLocal为变量在每个线程中都创建了一个副 ...
- javascript extend
interface Date{ addHours(h:number); addMinutes(m:number); format(str):string } interface String{ tri ...
- Luogu1641 SCOI2010生成字符串(组合数学)
NOI2018冒泡排序的一个子问题. #include<iostream> #include<cstdio> #include<cmath> #include< ...
- USACO Section 1.4 Mother's Milk 解题报告
题目 题目描述 有三个牛奶桶,三个桶的容积分别是A,B,C,最小为1,最大为20.刚开始只有第三个桶里面装满了牛奶,其余两个桶都是空的.我们现在可以将第三个桶中的牛奶往其他两个桶里面倒一些牛奶,然后还 ...
- 消除JQuery Mobile 列表样式右侧箭头
有时候我们看到JQM上面有一些呈现跟我们要的很像如下面这个Listview效果 程序代码如下: view sourceprint? 1.<ul data-role="listvie ...
- ASP.NET MVC —— Model之一模型模板
http://www.cnblogs.com/lzhp/archive/2013/03/25/2981650.html Mvc model系列文章主要分为三部分:Model Templates,Mod ...
- SHELL (2) —— Shell变量的核心基础知识和实践
摘自:Oldboy Linux运维——SHELL编程实战 Shell变量:用一个固定的字符串(也可能是字符.数字等的组合)代替更多.更复杂的内容,该内容里可能还会包含变量.路径.字符串等其它的内容. ...