首页 > 代码库 > Project Euler:Problem 77 Prime summations
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 <iostream> #include <string> using namespace std; int prime[1000]; //存储前1000个质数 bool vis[10000]; void getPrime() { int count = 0; memset(vis, 0, sizeof(vis)); for (int i = 2; i < 10000; i++) { if (!vis[i]) { if (count >= 1000) 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 < 1000; 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; else num++; } cout << num << endl; system("pause"); return 0; }
Project Euler:Problem 77 Prime summations
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。