原题:

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转义字符

    字符 转义字符 备注 & (ampersand) & 这个没什么特别的,几乎所有的地方都需要使用转义字符 > (greater-than character) > 在属性( ...

  2. Silverlight自动根据屏幕分辨率进行布局

    xaml: <UserControl x:Class="SLCenterLayout.MainPage" xmlns="http://schemas.microso ...

  3. 用户登录密码RSA加密后传输的实现,非明文密码传输

    在用户登录页面,用户输入密码后,在传送到服务器端时,为防止在密码传送过程中,被如360这种东东给拦截到, 需要在传送前对密码进行加密,然后再传送! 利用RSA加密,在客户端使用公钥对密码进行加密,在服 ...

  4. 深入剖析——float之个人见解

    浮动的原本作用仅仅是为了实现文字的环绕效果. 以下分别是html与css代码,显示效果如下图.因为两个div使用了float浮动属性,所以脱离了标准文档流.让父元素撑开高度,我们需要清除浮动. < ...

  5. Oracle收缩表空间

    可以使用 alter database datafile 'file path...' resize xM 的命令来缩小数据文件. SELECT 'alter database datafile '' ...

  6. 从零开始学ios开发(十二):Table Views(上)

    这次学习的控件非常重要且非常强大,是ios应用中使用率非常高的一个控件,可以说几乎每个app都会使用到它,它就是功能异常强大的Table Views.可以打开你的iphone中的phone.Messa ...

  7. 执行umount 命令的时候出现 device is busy

    执行umount 命令的时候出现 device is busy ,有人在使用这块磁盘 umount /dev/sde1 umount: /u01/app/oracle: device is busy ...

  8. 0x01第一个汇编程序

    ;将由text db 10,20,30,40定义的4个数相加,并输出其和. .386    ;指明指令集 .model flat,stdcall ;平坦模式,函数右边的参数先入栈 option cas ...

  9. 最火的.NET开源项目(转)

    综合类 微软企业库 微软官方出品,是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题, 如安全(Security).日志(Logging).数据访问(Data Access).配置管理( ...

  10. Kali Linux 优化过程

    修改输入法横向候选字 vim ~/.config/fcitx/conf fcitx-classic-ui.config 修改此行 为 false  :VerticalList=False   mb这玩 ...