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的更多相关文章

  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. 模拟Queue(wait/notify)

    BlockingQueue:顾名思义,首先它是一个队列,并且支持阻塞的机制,阻塞的放入和得到数据.我们要实现LinkedBlockingQueue下面的两个方法put和take. put(anObje ...

  2. A - Team

    Problem description One day three best friends Petya, Vasya and Tonya decided to form a team and tak ...

  3. java实现读取yaml文件,并获取值

    首先在项目src目录下新建一个test.yaml的文件. 代码如下: spring: application: name: cruncher datasource: driverClassName: ...

  4. 全文检索引擎及工具 Lucene Solr

    全文检索引擎及工具 lucence lucence是一个全文检索引擎. lucence代码级别的使用步骤大致如下: 创建文档(org.apache.lucene.document.Document), ...

  5. mui图片懒加载

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  6. 【PostgreSQL-9.6.3】如何实现非自动提交

    我们在使用psql工具操作数据库时,事务是自动提交的.也就是说,当我们执行完一条insert或者delete语句后,在不输入commit情况下,这条语句也是提交的.如果不想自动提交,可以使用以下两种方 ...

  7. 【python】os.getcwd和getcwdu

    print os.getcwd(), type(os.getcwd()) print os.getcwdu(), type(os.getcwdu()) 结果如下: C:\Users\Administr ...

  8. java Web(2)

    Servlet与web容器的配合: 1)客户端向Web服务器发起一个HTTP请求. 2)HTTP请求被Web服务器接受,如果请求的是静态页面,则由Web服务器负责处理.如果请求的是Java Web组件 ...

  9. node jsonwebtoken

     jsonwebtoken是node版本的JWT(JSON Web Tokens)的实现.1.什么是JWT?Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于J ...

  10. Apex语言(九)类的方法

    1.方法 方法是对象的行为.如下表: 看书,编程,打球就是方法. 2.创建方法 [格式] 访问修饰符 返回值类型 方法名(形式参数列表){ 方法体; } 访问修饰符:可以为类方法指定访问级别. 例如, ...