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 + 24
33 = 32 + 23 + 24
49 = 52 + 23 + 24
47 = 22 + 33 + 24

How many numbers below fifty million can be expressed as the sum of a prime square, prime cube, and prime fourth power?



素数幂三元组

最小的可以表示为一个素数的平方,加上一个素数的立方,再加上一个素数的四次方的数是28。实际上,在小于50的数中,一共有4个数满足这一性质:

28 = 22 + 23 + 24
33 = 32 + 23 + 24
49 = 52 + 23 + 24
47 = 22 + 33 + 24

有多少个小于五千万的数,可以表示为一个素数的平方,加上一个素数的立方,再加上一个素数的四次方?

解题

先求素数,筛选法求素数,题意就是 num = a^2+b^3+c^4 num《五千万,a、b、c都是素数

可以看出,a的值最大可以是sqrt(五千万),b值得最大值是五千万的立方根,c的最大值是五千万的四次方根

所以素数的个数是:sqrt(五千万)+1

下面就直接暴力求解了

注意主要:

在求 立方 和四次放的时候可能出现越界的情况,long也不可以,所以越界的时候 结果是小于0的,大于五千万的也要考虑的

还有一个就是一个数num 可能有好几种表示的形式,也就是说有重复的情况,,,由于开始没有想到重复的情况,让我看了好久才找出问题的,,题目就是的num的个数,不是求的其分解形式个数,,,所以把符合条件的数放在集合中,最后直接集合大小就是答案了。下面java程序输出结果前一个是没有去重的,后一个是去重后的才是答案的

package Level3;

import java.util.ArrayList;
import java.util.TreeSet;

public class PE087{
    static void run(){
        int MAX = 50000000;
        int limit = (int)Math.sqrt(MAX) + 1 ;
        ArrayList<Integer> prime = getPrime(limit);
        int count = 0;
        int size = prime.size();
        TreeSet<Long> set = new TreeSet<Long>();
        for(int a = 0;a< size;a++){
            int tmp1 = prime.get(a);
            long p1 = tmp1*tmp1*tmp1*tmp1;
            if(p1> MAX || p1 <0) break;
            for(int b =0;b<size;b++){
                int tmp2 = prime.get(b);
                long p2 = tmp2*tmp2*tmp2;
                if(p2> MAX || p2 <0 || p1+p2<0 || p1+p2> MAX) break;
                for(int c = 0;c<size;c++){
                    int tmp3 = prime.get(c);
                    long p3 = tmp3*tmp3;
                    if(p3> MAX || p3 <0 || p1+p2+p3<0 || p1+p2+p3> MAX) break;
                    long pp = p1+p2+p3;

                    if(pp <MAX && pp>0 && p1>0 && p2>0 && p3>0){
                        count++;
                        set.add(pp);
//                        if(pp<50)
//                        System.out.println(p1+" p2:"+p2 +" p3:"+p3+" pp:"+ pp);
                    }
                }
            }
        }
            System.out.println(count +"..."+set.size());
    }
//    1139575...1097343
//    running time=1s585ms
    static boolean isPrime(int n){
        if(n==2) return true;
        if(n<2) return false;

        for(int i=2;i<=Math.sqrt(n);i++)
            if(n%i==0)
                return false;
        return true;
    }
    static ArrayList<Integer> getPrime(int limit){
        ArrayList<Integer> prime = new ArrayList<Integer>();
        boolean isPrime = true;
        prime.add(2);
        for(int i=3;i<=limit;i++){
            isPrime = true;
            for(int j=0;j<prime.size();j++){
                if(i%prime.get(j) ==0){
                    isPrime = false;
                    break;
                }
            }
            if(isPrime == true){
                prime.add(i);
//                System.out.println(i);
            }
        }

        return prime;
    }

    public static void main(String[] args){
        long t0 = System.currentTimeMillis();
        run();
        long t1 = System.currentTimeMillis();
        long t = t1 - t0;
        System.out.println("running time="+t/1000+"s"+t%1000+"ms");

    }
}

Python

# coding=gbk
import time as time 

t0 = time.time()

def run():
    MAX = 50000000
    limit = int(MAX**0.5)
    set={}
    prime = getPrime(limit)
    for pi in prime:
        p1 = pi**4
        if p1>MAX:break
        for pj in prime:
            p2 = pj**3
            if p2>MAX:break
            for pk in prime:
                p3 = pk**2
                if p3>MAX:break
                if p1 + p2 + p3 <MAX:
                    set[p1+p2+p3] = 1
    print len(set)

#     1097343
# running time= 0.729000091553 s

def getPrime(limit):
    prime = [2]
    isPrime = True
    for i in range(2,limit):
        isPrime = True
        for p in prime:
            if i%p==0:
                isPrime = False
                break
        if isPrime == True:
            prime.append(i)
    return prime  

run()
t1 = time.time()
print "running time=",(t1-t0),"s"
时间: 2024-12-24 05:49:40

Project Euler 87 :Prime power triples 素数幂三元组的相关文章

Project Euler:Problem 87 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 + 24 33 = 32 + 23 + 24 49 = 52 + 23 + 24 47

Python练习题 037:Project Euler 009:毕达哥拉斯三元组之乘积

本题来自 Project Euler 第9题:https://projecteuler.net/problem=9 # Project Euler: Problem 9: Special Pythagorean triplet # A Pythagorean triplet is a set of three natural numbers, # a < b < c, for which, a**2 + b**2 = c**2 # For example, 3**2 + 4**2 = 9 +

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

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler 做题记录

Project Euler 太好玩了...(雾 Problem 675 设 \(\omega(n)\) 表示 \(n\) 的质因子个数,\(S(n)=\sum_{d|n}2^{\omega(d)}\),求 \(F(n)=\sum_{i=2}^nS(i!) \bmod (10^9+87)\). \(n=10^7\) solution \(n=\prod_{i=1}^kp_i^{e_i}\) \(S(n)=\prod_{i=1}^k(2e_i+1)\) 线性筛求出每个数的最小质因子之后就可以对 \(

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