The smallest number expressible as the sum of a prime square, Prime cube, and prime Fourth power is 28. In fact, there is exactly four numbers below fifty the can is expressed in such a-to:
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?
Approximate range of first judgment: sqrt (50000000) =7081
Find out all prime numbers between 2~7081
Then the three-layer loop facilitates all integers that can be expressed within 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 = pr IME[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;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Project Euler:problem Prime Power Triples