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 ...
随机推荐
- 用泛型的IEqualityComparer<T>接口去重复项
提供者:porschev 题目:下列数据放在一个List中,当ID和Name都相同时,去掉重复数据 ID Name 1 张三 1 李三 1 小伟 1 李三 2 李四 2 李武 ----- ...
- Berkeley DB数据处理
设计一个结构,利用Berkeley DB完成大数据的存储,备份,查询功能. 已有的储备: 1.Berkeley DB的基本操作. 2.数据转存后数据不丢失. 3.过百GB以上数据的存储. 数据流如下, ...
- Python开发【第一篇】Python基础之装饰器
写代码要遵循开发封闭原则,虽然在这个原则是用的面向对象开发,但是也适用于函数式编程,简单来说,它规定已经实现的功能代码不允许被修改,但可以被扩展,即: 封闭:已实现的功能代码块开放:对扩展开发 #s2 ...
- 金融系列4《PUTKEY指令》
用一个新的密钥替换一个已经存在的密钥:新密钥可以有与被替换的密钥相同的或不同的密钥版本号,但是必须与被替换的密钥有相同的密钥标识符. 用新密钥替换多个已经存在的密钥:新密钥可以有与被替换的密钥相同的或 ...
- ccache高速编译工具
ccache的主页:http://ccache.samba.org distcc的主页:http://distcc.samba.org 1.背景: 在处理一些规模相对较大的工程时,编译花费的时间可能会 ...
- 深入PHP EOF(heredoc)用法详解
介绍下使用EOF heredoc方式,输出长段内容的方法, <?php $name = '姓名'; print <<<EOT <html> <head> ...
- std::function赋值的几种方法
定义: #include <functional> std::function<void(const QString&)> myPrintFunction; 函数指针 ...
- sublime package
Sublime text 2/3 中 Package Control 的安装与使用方法 2014/05/23前端工具, 工具, 教程, 软件4条评论 Package Control 插件是一个方便 S ...
- EXT--columnWidth
在EXT 3.4API上没有查询到columnWidth这个配置项,但它却实实在在的在起作用,后来在ColumnLayout类查到它的信息: 上面的信息描述了采用了columnLayout布局的子面板 ...
- 【BZOJ】【1965】SHUFFLE 洗牌
扩展欧几里德+快速幂 每次转换位置:第x位的转移到2*x %(n+1)这个位置上 那么m次后就到了(2^m)*x %(n+1)这个位置上 那么找洗牌m次后在 l 位置上的牌就相当于解线性模方程: (2 ...