原题:

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的更多相关文章

  1. Project Euler 87 :Prime power triples 素数幂三元组

    Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and pr ...

  2. Project Euler 76:Counting summations

    题目链接 原题: It is possible to write five as a sum in exactly six different ways: 4 + 13 + 23 + 1 + 12 + ...

  3. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  4. Python练习题 035:Project Euler 007:第10001个素数

    本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime ...

  5. Python练习题 031:Project Euler 003:最大质因数

    本题来自 Project Euler 第3题:https://projecteuler.net/problem=3 # Project Euler: Problem 3: Largest prime ...

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

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

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

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

  8. Python练习题 047:Project Euler 020:阶乘结果各数字之和

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

  9. Python练习题 046:Project Euler 019:每月1日是星期天

    本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first o ...

随机推荐

  1. WPF 绑定四(层级绑定)

    xaml: <Window x:Class="WpfApplication1.Window4" xmlns="http://schemas.microsoft.co ...

  2. 选项切换条--第三方开源--SHSegmentControl

    SHSegmentControl在github上的项目主页地址:https://github.com/7heaven/SHSegmentControl SHSegmentControl使用简单,在xm ...

  3. WPF中的瀑布流布局(TilePanel)控件

    最近在用wpf做一个metro风格的程序,需要用到win8风格的布局容器,只能自己写一个了.效果如下 用法 : <local:TilePanel                          ...

  4. WPF 气泡尖角在左边、下面、右边、上面

    由于项目需要,在弄一个气泡提示框,根据网上资料,使用Path可以将气泡画出来,下面是我画出来的. 1.气泡尖角在左边的: <Path Stroke="Black" Strok ...

  5. eclipse安装pydev插件

    打开Eclipse,找到Help菜单栏,进入Install New Software…选项. 点击work with:输入框的旁边点击Add…,Name可以随便输入,Location是http://p ...

  6. [MySql] 设置了UTF8,中文存数据库中仍然出现问号

    运行命令:SHOW VARIABLES LIKE 'character_set_%'; 结果 'character_set_client', 'utf8' 'character_set_connect ...

  7. 微软职位内部推荐-SDE2 (Windows - Power)

    微软近期Open的职位: SDE2 (Windows - Power) Windows Partner Enablement team in Operating System Group is loo ...

  8. C++(MFC)编程一些注意事项

    一·书写问题 1.括号:左右大括号最好都放在左侧,这样可以很清楚大括号的看清配对情况以及作用域,便于检查也不易出错. 2.强制转换:强制转换表达式时一定要加括号,否则可能只转换了表达式中的单个量,可能 ...

  9. JS实现刷新iframe的方法

    <iframe src="1.htm" name="ifrmname" id="ifrmid"></iframe> ...

  10. MarkdownPad2添加目录(输出为HTML时可用)

    平时看书的时候懒得上网写在线博客,就在电脑上用了很长时间的MarkDownPad2来记录自己的心得笔记,等那天高兴了再把他们贴出来.界面清爽,是我使用它最重要的原因,但是MarkdownPad2导出的 ...