Project Euler:Product-sum numbers (problem 88) C++
A natural number, N, that can be written as the sum and product of a given set of at least two natural numbers, {a1, a2, ... , ak} is called a product-sum number: N = a1 + a2 + ... + ak = a1 × a2 × ... × ak.
For example, 6 = 1 + 2 + 3 = 1 × 2 × 3.
For a given set of size, k, we shall call the smallest N with this property a minimal product-sum number. The minimal product-sum numbers for sets of size, k = 2, 3, 4, 5, and 6 are as follows.
k=2: 4 = 2 × 2 = 2 + 2
k=3: 6 = 1 × 2 × 3 = 1 + 2 + 3
k=4: 8 = 1 × 1 × 2 × 4 = 1 + 1 + 2 + 4
k=5: 8 = 1 × 1 × 2 × 2 × 2 = 1 + 1 + 2 + 2 + 2
k=6: 12 = 1 × 1 × 1 × 1 × 2 × 6 = 1 + 1 + 1 + 1 + 2 + 6
Hence for 2≤k≤6, the sum of all the minimal product-sum numbers is 4+6+8+12 = 30; note that 8 is only counted once in the sum.
In fact, as the complete set of minimal product-sum numbers for 2≤k≤12 is {4, 6, 8, 12, 15, 16}, the sum is 61.
What is the sum of all the minimal product-sum numbers for 2≤k≤12000?
求积和数的一道题目,大多都是递归。
继续推导可以发现,f(k)的取值在[k,2k]之间。
可以推出,k=num-因子和+(num-因子和)*1;
那好了,就是写个<set>去重,写出递归函数就可以。
#include<iostream>
#include<set>
using namespace std;
set<int> Q;
set<int>::iterator it;
bool re(int x,int y,int z);
int getn(int n)
{
for(int k=n+1;k<=2*n;k++) //k的取值 k---2k
{
if(re(k,k,n)) //num, sum, digit
return k;
}
}
bool re(int x,int y,int z)
{
//cout<<x<<" "<<y<<" "<<z<<endl;
if(y<z)
return 0;
if(x==1)
return y==z;
if(z==1)
return x==y;
for(int i=2;i<=x;i++)
{
if(x%i==0)
{
// cout<<" i="<<i<<endl;
if(re(x/i,y-i,z-1))
return 1;
} }
return 0;
}
int main()
{
int n;
long long s=0;
for(int i=2;i<=12000;i++)
{
n=getn(i);
Q.insert(n); //此处可以直接判断 insert()的返回值,求和。
}
for(it=Q.begin();it!=Q.end();it++)
{
s+=*it;
}
cout<<s<<endl;
} //execution time : 21.385 s
Project Euler:Product-sum numbers (problem 88) C++的更多相关文章
- Project Euler:Problem 88 Product-sum numbers
A natural number, N, that can be written as the sum and product of a given set of at least two natur ...
- 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 61 Cyclical figurate numbers
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...
- Project Euler:Problem 42 Coded triangle numbers
The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...
- 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 28 Number spiral diagonals
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...
- Project Euler:Problem 32 Pandigital products
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 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 34 Digit factorials
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...
随机推荐
- Linux命令的学习
mkdir -p 创建目录 (make directorys) p递归创建 ls -l(long)d(direcitory)显示目录或者文件 cd 切换目录 从"/"开始目录,/ ...
- js面试题知识点全解(一变量类型和计算)
1.js中使用typeof能得到哪些类型 2.何时使用===和== 3.js中的内置函数 4.js变量按存储方式区分为哪些类型,并描述其特点 5.如何理解json 以下对这些问题的知识点做一些总结: ...
- python函数参数中带有默认参数list的坑
在python中函数参数中如果带有默认参数list遇到问题 先看一段代码 def f(x,l=[]): for i in range(x): l.append(i*i) print(l) print( ...
- (转载)公开的海量数据集 Public Research-Quality Datasets
转载自:http://rensanning.iteye.com/blog/1601663 海量数据数据集 海量数据(又称大数据)已经成为各大互联网企业面临的最大问题,如何处理海量数据,提供更好的解决方 ...
- postman接口测试工具完整教程
第一部分:基础篇 postman:4.5.11.安装postman进入postman官网,如果是mac系统可以直接点击mac app安装 如果是windows的话,需要在windows下安装chrom ...
- ionic实战系列(二):使用cordova插件
本章主要关注cordova的各种插件,利用好手机(移动设备)的原生功能.首先cordova是一个将web网页内嵌到原生app的平台(核心功能),然后cordova拥有的插件系统扩展了核心功能. Cor ...
- [读书笔记] 三、搭建基于Spring boot的JavaWeb项目
一.POM <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3. ...
- Java开发相关命名规范
JAVA文件命名规范 1.类命名 抽象类以 Abstract 或者 Base 开头.异常类以 Exception 作为后缀.枚举类以 Enum 作为后缀.工具类以 Utils 作为后缀(相应的包名最后 ...
- ASP.Net Core WebApi几种版本控制对比
版本控制的好处: (1)助于及时推出功能, 而不会破坏现有系统. (2)它还可以帮助为选定的客户提供额外的功能. API 版本控制可以采用不同的方式进行控制,方法如下: (1)在 URL 中追加版本或 ...
- Python学习日志_2017/09/08
今天早晨学习了<Head First :HTML and CSS>:学习了两个章节,感觉从基础学习特别的踏实,能看懂的同时踏踏实实的锻炼了基础的能力.我个人认为无论哪个行业,最重要的永远是 ...