Project Euler 88:product-sum numbers product and sum

Source: Internet
Author: User

Product-sum numbers

A natural number, N, that can is written as the sum and product of a given set of at least the natural numbers, {a1, A2, ... , AK} is called a product-sum number:n = a1 + A2 + ... + ak = A1xa2x...xak.

For example, 6 = 1 + 2 + 3 = 1x2x3.

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 is as follows.

K=2:4 = 2x2 = 2 + 2
K=3:6 = 1x2x3 = 1 + 2 + 3
K=4:8 = 1x1x2x4 = 1 + 1 + 2 + 4
K=5:8 = 1x1x2x2x2 = 1 + 1 + 2 + 2 + 2
K=6:12 = 1x1x1x1x2x6 = 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 a counted once in the sum.

In fact, the AS-the complete set of minimal product-sum numbers for 2≤k≤12 is {4, 6, 8, 61, +, +}, the sum is.

What's the sum of the minimal product-sum numbers for 2≤k≤12000?

Accumulate and Sum

If the natural number n can be simultaneously expressed as a group of at least two natural number {A1, A2, ..., AK} the product sum and sum, also namely n = A1 + a2 + ... + ak = A1xa2x...xak, then n is called the product and the accumulation.

For example, 6 is an accumulation and sum, because 6 = 1 + 2 + 3 = 1x2x3.

Given the size of the set K, we call the minimum n value of the above property to be the minimum product sum. When k = 2, 3, 4, 5, 6 o'clock, the minimum product sum is as follows:

K=2:4 = 2x2 = 2 + 2
K=3:6 = 1x2x3 = 1 + 2 + 3
K=4:8 = 1x1x2x4 = 1 + 1 + 2 + 4
K=5:8 = 1x1x2x2x2 = 1 + 1 + 2 + 2 + 2
K=6:12 = 1x1x1x1x2x6 = 1 + 1 + 1 + 1 + 2 + 6

Therefore, for 2≤k≤6, the sum of all the minimum product counts is 4+6+8+12 = 30; Note 8 is counted only once.

For 2≤k≤12, the set of all minimum product and sum is known to be {4, 6, 8, 12, 15, 16}, these numbers are 61.

For 2≤k≤12000, what is the sum of all the minimum product numbers?

Solving

Product of the number of k and = = K

For the smallest number in the K-time.

The topic requires 2≤k≤12000, the time of the smallest plot no and

Procedures in the reference, detailed program notes

Java

 PackageLevel3;ImportJava.util.Set;ImportJava.util.TreeSet; Public classpe088{Static voidrun () {intKmin = 2; intKmax = 12000; intsum = 0; Set<Integer> set =NewTreeset<integer>();  for(intk=kmin;k<=kmax;k++){            intMinn =Getmin (k); if(SET.ADD (Minn)) sum+=Minn;    } System.out.println (sum); }    //Find k for the smallest n    Static intGetmin (intk) {         for(intn=k+1;; n++){            if(check (n,n,k))returnN; }    }    //A number of numbers that are split into k and or K numbers.//prod Multiply//Sum and//at first, these two numbers are equal, prod or sum split into K-parts.//here is the recursive method, when prod2 = Prod1 * A; sum2 = Sum1-a//The next round of check (PROD2,SUM2,K-1) can be detected//here with recursion is also because possible out of 8 = 2*2*2*1*1 = 2+2+2+1+1 form, in which there are several identical cases in the multiplicative child//End Condition: When the multiplier is 1, sum = = k K 1 sum is sums//k=1 the end of the time. return prod = = Sum//next recursion is required: d<= prod k-1<= sum-d The following procedure is obviously    Static BooleanCheckintProdintSumintk) {        if(Sum <k)return false; if(prod = = 1)returnsum==K; if(k==1)returnProd = =sum;  for(intD =2;d<= prod && sum-d>=k-1;d++){            if(prod%d==0){                if(Check (prod/d,sum-d,k-1))                     return true; }        }        return false; }//7587457//running Time=1s577ms     Public Static voidMain (string[] args) {LongT0 =System.currenttimemillis ();        Run (); LongT1 =System.currenttimemillis (); Longt = T1-t0; System.out.println ("Running Time=" +t/1000+ "s" +t%1000+ "MS"); }}

Reference links

N[k] means minimal product-sum numbers for size=k

The upper bound of N[k] is 2*k, because 2*k can always be decomposed into 2*k, then 2*k=k+2+ (1) * (k-2)

Obviously the nether of N[k] is K

For a number of num-factorization after decomposition, the number of factors is the sump of the factors of product

The number of 1 that needs to be added is num-sump, so size k=num-sump+product

===============================================

That's a good understanding.

In the case of factoring

N[K] is a number of numbers broken down into the number of K and K number of the product of the minimum value

I link above the program's understanding is through the factorization, continuously reduces the value of n[k], the final value is the smallest, but the recursion after the program is not understood ...

#CODING=GBKImport time as timedefrun2 (): KMax= 12000N= [2*kmax forIinchrange (KMax)]defGETPSN (Num,sump,product,start): K= Num-sump +ProductifK <KMax:ifnum<N[k]: n[k]=Num forIinchRange (Start,kmax//num): GETPSN (num*i,sump+i,product + 1, i) GETPSN (1,1,1,2) ans= SUM (Set (n[2:])) Printans#7587457#running time= 0.266000032425 sdefrun (): Kmin= 2KMax= 12000Res=[]     forKinchRange (kmin,kmax+1): Minn=Getminn (k)ifMinn not inchres:res.append (Minn)PrintSUM (Minn)defGetminn (k): N= k + 1 while(True):ifCheck (n,n,k):returnN N+=1defCheck (prod,sum,k):ifSum<k:returnFalseifProd = = 1:returnsum==kifK==1:returnProd = =sum forDinchRange (2, prod):ifSum-d>=k-1 andprod%d = =0:ifCheck (prod/d,sum-d,k-1):                returnTruereturnFalset0=time.time () run2 () T1=time.time ()Print "running Time=", (T1-T0),"s"            

Project Euler 88:product-sum numbers product and sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.