Project Euler:problem Prime Power Triples

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.