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 = 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?

先判断数的大致范围:sqrt(50000000)=7081

求出2~7081之间的所有质数

然后三层循环便利出所有能表示出的50000000以内的整数

#include <iostream>
#include <string>
#include <map>
using namespace std;

bool isPrime[7081];
int prime[2000];

void judge()
{
	for (int i = 2; i < 7081; i++)
	{
		if (isPrime[i])
		{
			for (int j = i; j*i < 7081; j++)
				isPrime[j*i] = 0;
		}
	}
}

int getPrime()
{
	int count = 0;
	for (int i = 2; i < 7081; i++)
	{
		if (isPrime[i])
		{
			prime[count++] = i;
		}
	}
	return count;
}

int main()
{
	memset(isPrime, true, sizeof(isPrime));
	judge();
	int num = getPrime();
	map<int, int>mp;
	for (int i = 0; i < num; i++)
	{
		int a = prime[i] * prime[i] * prime[i] * prime[i];
		if (a>50000000)
			break;
		for (int j = 0; j < num; j++)
		{
			int b = prime[j] * prime[j] * prime[j];
			if (a + b>50000000)
				break;
			for (int k = 0; k < num; k++)
			{
				int c = prime[k] * prime[k];
				if (a + b + c>50000000)
					break;
				mp[a + b + c]++;
			}
		}
	}

	cout << mp.size() << endl;
	system("pause");
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-28 00:59:22

Project Euler:Problem 87 Prime power triples的相关文章

Project Euler:Problem 77 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? #include <i

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:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

Project Euler:Problem 41 Pandigital prime

We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? #include <iostream> #incl

Project Euler:Problem 50 Consecutive prime sum

The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds t

Project Euler:Problem 16 Power digit sum

215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? #include <iostream> #include <string> using namespace std; int main() { string s = "1"; for (int i = 1; i <= 1000;

Project Euler:Problem 69 Totient maximum

Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n. For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and relatively prime to nine, φ(9

Project Euler:Problem 18 Maximum path sum I

By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top to bottom of the triangle below

Project Euler:Problem 55 Lychrel numbers

If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindromes so quickly. For example, 349 + 943 = 1292, 1292 + 2921 = 4213 4213 + 3124 = 7337 That is, 349 took three iterations to arrive at a palindrome. Al