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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Project Euler:Problem 55 Lychrel numbers
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic. Not all numbers produce palindr ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Project Euler:Problem 37 Truncatable primes
The number 3797 has an interesting property. Being prime itself, it is possible to continuously remo ...
随机推荐
- xml转换成数组array
直接上代码,成功转换 if($data){ //返回来的是xml格式需要转换成数组再提取值,用来做更新 $startnum = strpos($data,"<xml>" ...
- 猜拳游戏项目(涉及知识点Scanner、Random、For、数组、Break、Continue等)
package day03.d3.xunhuankongzhi; import java.util.Scanner; public class CaiQuan { public static void ...
- Eclipse 每次ctrl-c ctrl-v 就变慢?
继续闲着,所以继续写 大小: 60.7 KB 查看图片附件
- 1.0 windows10系统安装步骤(1)
1.0 windows10系统安装步骤(1) 根据自己对笔记本系统的折腾,为了方便他人系统的安装,故总结笔记本系统的安装步骤 目录: 1.0 [windows10系统安装步骤(1)] 2.0 Linu ...
- SLAM: SLAM的发展历程(WIKI)
参考维基百科: https://en.wikipedia.org/wiki/Simultaneous_localization_and_mapping 你们叫他SLAM,我还是习惯叫他三维重建.... ...
- C#抽奖算法
摘自网络 static void Main(string[] args) { //各物品的概率保存在数组里 ]{ 0.5f, 0.5f, , }; //单次测试 //Console.WriteLine ...
- webpack核心提炼
基本是学习的时候在网上整理的资料,并非自己原创,这篇文章的的主要目的是记录webpack.config.js的配置方式.可能也有不少错误,欢迎指正!! 一.应用场景 前端模块化开发.功能拓展.css预 ...
- java将父类所有的属性COPY到子类中
public class FatherToChildUtils { /* * 将父类所有的属性COPY到子类中. * 类定义中child一定要extends father: * 而且child和fat ...
- Android内存优化————加载长图
项目中总会遇到加载长图的需求,图片的长度可能是手机长度的很多倍,也就是需要通过滑动来查看图片.比较简单的实现方式就是使用ScrollView来加载长图,但是这样做有一个很严重的问题,就是内存消耗严重. ...
- 【剑指Offer】32、把数组排成最小的数
题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. ...