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?

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int prime[1000]; //存储前1000个质数
  6. bool vis[10000];
  7.  
  8. void getPrime()
  9. {
  10. int count = 0;
  11. memset(vis, 0, sizeof(vis));
  12. for (int i = 2; i < 10000; i++)
  13. {
  14. if (!vis[i])
  15. {
  16. if (count >= 1000)
  17. break;
  18. prime[count++] = i;
  19. for (int j = i*i; j < 10000; j += i)
  20. vis[j] = 1;
  21. }
  22. }
  23. }
  24. int main()
  25. {
  26. getPrime();
  27. int *ways;
  28. int num = 2;
  29. while (true)
  30. {
  31.  
  32. ways = new int[num+1];
  33. for (int i = 0; i < num + 1; i++)
  34. ways[i] = 0;
  35. ways[0] = 1;
  36. for (int i = 0; i < 1000; i++)
  37. {
  38. for (int j = prime[i]; j <= num; j++)
  39. {
  40. ways[j] += ways[j - prime[i]];
  41. }
  42. }
  43. //cout << num <<" " << ways[num]<< endl;
  44. if (ways[num]>5000)
  45. break;
  46. else
  47. num++;
  48. }
  49. cout << num << endl;
  50.  
  51. system("pause");
  52. return 0;
  53. }

Project Euler:Problem 77 Prime summations的更多相关文章

  1. 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 ...

  2. Project Euler:Problem 76 Counting summations

    It is possible to write five as a sum in exactly six different ways: 4 + 1 3 + 2 3 + 1 + 1 2 + 2 + 1 ...

  3. 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 o ...

  4. Project Euler:Problem 55 Lychrel numbers

    If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...

  5. Project Euler:Problem 47 Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The ...

  6. Project Euler:Problem 63 Powerful digit counts

    The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...

  7. Project Euler:Problem 86 Cuboid route

    A spider, S, sits in one corner of a cuboid room, measuring 6 by 5 by 3, and a fly, F, sits in the o ...

  8. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  9. Project Euler:Problem 37 Truncatable primes

    The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...

随机推荐

  1. xml转换成数组array

    直接上代码,成功转换 if($data){ //返回来的是xml格式需要转换成数组再提取值,用来做更新 $startnum = strpos($data,"<xml>" ...

  2. 猜拳游戏项目(涉及知识点Scanner、Random、For、数组、Break、Continue等)

    package day03.d3.xunhuankongzhi; import java.util.Scanner; public class CaiQuan { public static void ...

  3. Eclipse 每次ctrl-c ctrl-v 就变慢?

    继续闲着,所以继续写 大小: 60.7 KB 查看图片附件

  4. 1.0 windows10系统安装步骤(1)

    1.0 windows10系统安装步骤(1) 根据自己对笔记本系统的折腾,为了方便他人系统的安装,故总结笔记本系统的安装步骤 目录: 1.0 [windows10系统安装步骤(1)] 2.0 Linu ...

  5. SLAM: SLAM的发展历程(WIKI)

    参考维基百科: https://en.wikipedia.org/wiki/Simultaneous_localization_and_mapping 你们叫他SLAM,我还是习惯叫他三维重建.... ...

  6. C#抽奖算法

    摘自网络 static void Main(string[] args) { //各物品的概率保存在数组里 ]{ 0.5f, 0.5f, , }; //单次测试 //Console.WriteLine ...

  7. webpack核心提炼

    基本是学习的时候在网上整理的资料,并非自己原创,这篇文章的的主要目的是记录webpack.config.js的配置方式.可能也有不少错误,欢迎指正!! 一.应用场景 前端模块化开发.功能拓展.css预 ...

  8. java将父类所有的属性COPY到子类中

    public class FatherToChildUtils { /* * 将父类所有的属性COPY到子类中. * 类定义中child一定要extends father: * 而且child和fat ...

  9. Android内存优化————加载长图

    项目中总会遇到加载长图的需求,图片的长度可能是手机长度的很多倍,也就是需要通过滑动来查看图片.比较简单的实现方式就是使用ScrollView来加载长图,但是这样做有一个很严重的问题,就是内存消耗严重. ...

  10. 【剑指Offer】32、把数组排成最小的数

      题目描述:   输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323.   ...