Project Euler:problem Prime summations

Source: Internet
Author: User

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 being written as the sum of primes in over five thousand different ways?



#include <iostream> #include <string>using namespace Std;int prime[1000];  Stores the first 1000 prime numbers bool Vis[10000];void getprime () {int count = 0;memset (Vis, 0, sizeof (VIS)), for (int i = 2; i < 10000; i++) {i F (!vis[i]) {if (Count >=) break;prime[count++] = i;for (int j = i*i; J < 10000; J + = i) vis[j] = 1;}}  int main () {getprime (); int *ways;int num = 2;while (true) {ways = new Int[num+1];for (int i = 0; i < num + 1; i++) Ways[i] = 0;ways[0] = 1;for (int i = 0; i <; i++) {for (int j = prime[i]; j <= Num; j + +) {Ways[j] + = ways[j-prime[i]]; }}//cout << num << "<< ways[num]<< endl;if (ways[num]>5000) break;elsenum++;} cout << num << endl;system ("pause"); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Project Euler:problem Prime summations

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.