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
#     71
# running time 0.00999999046326 s
if __name__==‘__main__‘:
    t0 = time.time()
    limit = 5000
    dp(limit)
    print"running time",(time.time() - t0),"s"
时间: 2024-11-12 20:19:18

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 prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way: 28 = 22 + 23 + 2433 = 32 + 23 + 2449

Project Euler 76:Counting summations

题目链接 原题: It is possible to write five as a sum in exactly six different ways: 4 + 13 + 23 + 1 + 12 + 2 + 12 + 1 + 1 + 11 + 1 + 1 + 1 + 1 How many different ways can one hundred be written as a sum of at least two positive integers? 翻译: 加和计数 将5写成整数的和有

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

本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime # By listing the first six prime numbers: # 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. # What is the 10 001st prime number? # Answer

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

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b

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

本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial digit sum n! means n × (n ? 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! i

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

本题来自 Project Euler 第19题:https://projecteuler.net/problem=19 ''' How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)? Answer: 171 ''' from datetime import * firstDay = date(1901,1,1) lastDay = date(

Python练习题 034:Project Euler 006:和平方与平方和之差

本题来自 Project Euler 第6题:https://projecteuler.net/problem=6 # Project Euler: Problem 6: Sum square difference # The sum of the squares of the first ten natural numbers is, # 1**2 + 2**2 + ... + 10**2 = 385 # The square of the sum of the first ten natur

Python练习题 042:Project Euler 014:最长的考拉兹序列

本题来自 Project Euler 第14题:https://projecteuler.net/problem=14 ''' Project Euler: Problem 14: Longest Collatz sequence The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule

Python练习题 041:Project Euler 013:求和、取前10位数值

本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # Work out the first ten digits of the sum of the following one-hundred 50-digit numbers. # Answer: 5537376230 numbers = '''371072875339021027987979982