Project Euler 77:Prime summations
Prime summations
It is possible to write ten as the sum of primes in exactly five different ways:
7 + 3
5 + 5
5 + 3 + 2
3 + 3 + 2 + 2
2 + 2 + 2 + 2 + 2
What is the first value which can be written as the sum of primes in over five thousand different ways?
素数加和
将10写成素数的和有5种不同的方式:
7 + 3
5 + 5
5 + 3 + 2
3 + 3 + 2 + 2
2 + 2 + 2 + 2 + 2
写成素数的和有超过五千种不同的方式的数最小是多少?
思路:
动态规划题目
我直接网上找的代码
但是大家写的好多都一样的
Java程序:
package Level3; import java.util.ArrayList;
import java.util.Iterator; public class PE077 { void run(){
int limit = 5000;
dp(limit);
} void dp(int limit){
ArrayList<Integer> plist = listPrime(limit/5);
int target = 2;
while(true){
int[] ways = new int[target+1];
ways[0] = 1;
for(int i=0;i<plist.size();i++){
for(int j=(int) plist.get(i);j<=target;j++)
ways[j] += ways[j-(int) plist.get(i)];
}
// System.out.println(target+" " + ways[target]);
if(ways[target] > limit) break;
target++;
}
System.out.println(target); }
// 71
// running time=0s7ms
ArrayList<Integer> listPrime(int limit){
int prime[] = new int[limit];
ArrayList<Integer> plist = new ArrayList<Integer>();
boolean isPrime = true;
prime[0]=2;
plist.add(2);
int p=1;
for(int i=2;i<limit;i++){
isPrime = true;
Iterator<Integer> it = plist.iterator();
while(it.hasNext() &&isPrime){
int prm=it.next();
if(i%prm==0){// 说明 i 不是素数
isPrime = false;
break;
}
} if(isPrime==true)
plist.add(i);
} return plist; } public static void main(String[] args) {
long t0 = System.currentTimeMillis();
new PE077().run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms");
} }
Python程序:
import time def sieve(limit):
primes= []
is_prime = True
primes.append(2)
for i in range(3,limit):
is_prime = True
for ps in primes:
if i%ps ==0:
is_prime = False
break
if is_prime==True:
primes.append(i)
return primes def dp(limit):
primes = sieve(1000)
target = 2
while True:
ways = [1] + [0]*target
for prime in primes:
for j in range(prime,target+1):
ways[j] += ways[j-prime]
if ways[target]> limit:
break
target+=1
print target
#
# running time 0.00999999046326 s
if __name__=='__main__':
t0 = time.time()
limit = 5000
dp(limit)
print"running time",(time.time() - t0),"s"
Project Euler 77:Prime summations的更多相关文章
- Project Euler 87 :Prime power triples 素数幂三元组
Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and pr ...
- Project Euler 76:Counting summations
题目链接 原题: It is possible to write five as a sum in exactly six different ways: 4 + 13 + 23 + 1 + 12 + ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- Python练习题 035:Project Euler 007:第10001个素数
本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime ...
- Python练习题 031:Project Euler 003:最大质因数
本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime ...
- 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 ...
- Python练习题 047:Project Euler 020:阶乘结果各数字之和
本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial ...
- Python练习题 046:Project Euler 019:每月1日是星期天
本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...
随机推荐
- PHPcms 摘要
一 常量 /** * 主要定义了路径常量,项目中经常用到 **/ define('PHPCMS_PATH',dirname(__FILE__).DIRECTORY_SEPARATOR);// 项目 ...
- python: 实现sha1小工具
File1: sha1.py File2: sha1.bat ------------------ File1: sha1.py import hashlib import os,sys def Ca ...
- 使用dxNavBar动态创建应用程序菜单
一.如何动态创建dxNavBar内容: function TMain.GetAcitonByCaption(const aCategory,aCaption: string): Integer; va ...
- How to understand ReferenceGroup control on Form[AX2012]
在AX2012的Form开发中,微软引入了新的控件ReferenceGroup,它用在Lookup其他表RecId的时候显示更人性化的字段,它的使用还必须从表的索引说起.AX2012的表有这些索引(h ...
- 如何访问Microsoft Azure Storage
首先先要创建存储账户 http://www.cnblogs.com/SignalTips/p/4119128.html 可以通过以下的几个方式访问 通过Visual Studio 2013 Commu ...
- 使用Qpython3制作老版天翼飞TP路由器拨号脚本
#幻境拨号python版 #by 1414641776 account='xxxxxx@96301' password='xxxxx' # 路由器脚本 def sendToRoute(account, ...
- CentOS安装 pure-ftpd
yum -y install pam-devel cd /usr/local .tar.gz cd pure-ftpd- ./configure --prefix=/usr/local/pure-ft ...
- 这些年,我收集的JavaScript代码
这些年,我收集的JavaScript代码(一) http://www.cnblogs.com/jscode/archive/2012/07/25/2605395.html 这些年,我收集的JavaSc ...
- vs2010旗舰版后,运行调试一个项目时调试不了,提示的是:无法使用“pc”附加到应用程序“webdev.webserver40.exe(PID:2260”
具体问题描述: vs2010旗舰版后,运行调试一个项目时调试不了,能编译,按ctrl+f5 可以运行,但是就是调试就不行,提示的是:无法使用“pc”附加到应用程序“webdev.webserver40 ...
- WPF中使用ValueConverter来实现“范围条件触发器”
在WPF中,我们知道界面层可以通过Trigger触发器实现“条件”——“赋值”的功能 属性触发器Property Trigger:当Dependency Property的值发生改变时触发.数据触发器 ...