A natural number, N, that can be written as the sum and product of a given set of at least two natural numbers, {a1a2, ... , 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?

n[k]表示minimal product-sum numbers for size=k

n[k]的上界为2*k,由于2*k总是能分解成2*k,然后2*k=k+2+(1)*(k-2)

显然n[k]的下界为k

对于一个数num   因式分解后因子个数为product   这些因子的和为sump

则须要加入的1的个数为num-sump,所以size k=num-sump+product

maxk = 12000
n=[2*maxk for i in range(maxk)] def getpsn(num,sump,product,start):
#print(num,' ',sump,' ',product)
k = num - sump + product
if k < maxk:
if num < n[k]:
n[k] = num
for i in range(start,maxk//num * 2): #控制num<=2*maxk
getpsn(num * i,sump + i,product + 1,i) getpsn(1,1,1,2)
ans=sum(set(n[2:]))
print(ans)

Project Euler:Problem 88 Product-sum numbers的更多相关文章

  1. Project Euler:Problem 61 Cyclical figurate numbers

    Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...

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

  3. Project Euler:Problem 55 Lychrel numbers

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

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

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

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

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

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

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

随机推荐

  1. [JavaEE] DWR框架实现Ajax

    Ajax是时下比较流行的一种web界面设计新思路,其核心思想是从浏览器获取XMLHttp对象与服务器端进行交互. DWR(Direct Web Remoting)就是实现了这种Ajax技术的一种web ...

  2. Kafka .NET操作

    Kafaka .NET连接 Kafka目前主流在用的.NET客户端有两个:一个是kafka-net,另外一个是Confluent.Kafka,这里给出使用示例: kafka-net示例: public ...

  3. BZOJ 1116 并查集

    思路: 如果 每个联通块 边数>=点数 就OK 用并查集搞 //By SiriusRen #include <cstdio> #include <cstring> #in ...

  4. css 画三角形

    <div class='triangle-rihgt'></div> <div class='triangle-top'></div> <div ...

  5. C# 2.0新加特性

    泛型(Generics) 泛型是CLR 2.0中引入的最重要的新特性,使得可以在类.方法中对使用的类型进行参数化. 例如,这里定义了一个泛型类: class MyCollection<T> ...

  6. Windows Phone - 按钮/button 控件

    System.Windows.Controls.Button   button控件一.button控件的各种样式的展示可以通过 …… 来给控件定义公共的样式调用样式的方法:在Button控件上添加样式 ...

  7. Qt5.2 for Android 配置及部署到手机运行

    使用DNK编程也没有那么难,使用QT为安卓跨平台编程需要安装NDK,SDK通过NDK调用C++程序,偶尔能提高一些效率. SDK下载地址:http://developer.android.com/sd ...

  8. ie6 中文字符编码 出现的问题解决

    0 前言   公司要求网页要支持ie6.7.8,结果我的设备在ie7.8.9均工作正常,而在ie6时就出现各种奇怪的错误,在这个背景下,开始动手解决网页不兼容ie6的情况.   1 搭建ie6的实验平 ...

  9. RabbitMQ学习之集群模式

    由于RabbitMQ是用erlang开发的,RabbitMQ完全依赖Erlang的Cluster,因为erlang天生就是一门分布式语言,集群非常方便,但其本身并不支持负载均衡.Erlang的集群中各 ...

  10. jQuery添加新的元素

    append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元素之前插入内容 $(&quo ...