Project Euler #254: Sums of Digit Factorials

  • + 22 comments

    Here is some G(i) values.
    None of them contain a digit 0, a pair of 11 nor tripple of 222.
    The digits of all G(i) values are always in increasing(or equal) order.

    G[1] = 1; G[11]= 26; G[21]= 349; G[31]=  229; G[41]=     2355679;
    G[2] = 2; G[12]= 44; G[22]=1349; G[32]= 1229; G[42]=      344479;
    G[3] = 5; G[13]=144; G[23]=2349; G[33]=   39; G[43]=     1344479;
    G[4] =15; G[14]=256; G[24]=  49; G[34]=  139; G[44]=     2378889;
    G[5] =25; G[15]= 36; G[25]= 149; G[35]=  239; G[45]=    12378889;
    G[6] = 3; G[16]=136; G[26]= 249; G[36]= 1239; G[46]=   133378889;
    G[7] =13; G[17]=236; G[27]=   9; G[37]=13339; G[47]= 2356888899L;
    G[8] =23; G[18]= 67; G[28]=  19; G[38]=23599; G[48]=12356888899L;
    G[9] = 6; G[19]=167; G[29]=  29; G[39]= 4479;
    G[10]=16; G[20]=267; G[30]= 129; G[40]=14479;
    
    • + 5 comments

      how you find G[ greater than 45]? i got G[1-45] and at G[46] got runtime error ....

      • + 1 comment
        [deleted]
        • + 1 comment

          but we need value of g(i) where i can be 100 also

          • + 0 comments

            Agree, this problem requires a lot more thinking then being able to compute the firsts few G(i) values.

            Answering 100000 queries which ask the sum all digits of G(i) values between 1 and 10^18 mod m does indeed involve dicovering related concepts.

      • + 1 comment

        I'm having the same problem! I am using int64 for storing the values. g1-45 are fast. After that it's very slow.

        • + 0 comments

          it is: include iostream

      • + 1 comment

        use BigInteger , it might help

        • + 1 comment

          try coding in python....you might not get any overflow errors

          • + 0 comments

            the issue is time......if better optimization could be done then the code would run.....python is itself slow its not the answer

      • + 0 comments

        Yesss same with me after 47 the processing time becomes hugee

      • + 0 comments

        same here after I give input greater than 45 it shows timeout error

    • + 1 comment

      Hey !!

      Can you please tell me something briefly about : 1) Input Values 2) Output Values

      I am not able get it in problem statement .

      • + 4 comments
        g(1)= 1, sf( 1) = s(   1!) = s(  1) =     1 = 1
        g(2)= 2, sf( 2) = s(   2!) = s(  2) =     2 = 2
        g(3)= 5, sf( 5) = s(   5!) = s(120) = 1+2+0 = 3
        g(4)=15, sf(15) = s(1!+5!) = s(121) = 1+2+1 = 4
        There is no number smaller than 15 which yeild 4.
        

        Let's take 1 query at a time and ignore the modulo.
        n=4 would mean :

        sg(1)+sg(2)+sg(3)+sg(4)
         s(1)+ s(2)+ s(5)+s(15)
           1 +   2 +   5 + 1+5
        
        • + 0 comments

          Okay got it !!!!! Just please tell me more about input values

        • + 0 comments

          how to find g(4)=15

        • + 0 comments

          What if the value exceeds more than 4 then should i have to find the g(7) and then put it to add the no.

        • + 0 comments

          g(3)= 5, sf( 5) = s( 5!) = s(120) = 1+2+0 = 3 how 121 will come? someone please elaborate this.

    • + 3 comments

      None of them contain a digit 0, a pair of 11 nor tripple of 222.

      Are these actual constraints in the problem? Just curious, how did you figure this out?

      These are really big hints.

      • + 1 comment

        Since all digits of n are evaluated independently, n is not a regular integer. You can see n as a combination with repetition of digits 0-9 where the order does not matter.
        Also the goal is to have the smallest value.
        #0 > 1# & #! + 0! == 1! + #!, therefore always use 1 instead of 0.
        11 > 2 & 1! + !1 == 2!, therefore always use 2 instead of 1 & 1.
        222 > 3 & 2! + 2! + 2! == 3!...

        • + 0 comments

          Ahh I see! Thanks! That make sense :) So you can extend this even further to N amount of digits. 3333 is the same as 4, so on and so forth.

      • + 0 comments

        Ahh I see! Thanks! That make sense :) So you can extend this even further to N amount of digits. 3333 is the same as 4, so on and so forth.

      • + 0 comments

        Actually, g(1) should be 0, since 0!=1, and 0 is an integer..... However this problem considers g(1) as 1.... which is indeed, not the smallest integer which you can sum it's factorials to get 1.... 0 is.

    • + 1 comment

      g[20] is 267 or 266 ?

      • + 1 comment
        sf(266)=s(2!+6!+6!)=s(2+720+720) =s(1442)=1+4+4+2=11
        sf(267)=s(2!+6!+7!)=s(2+720+5040)=s(5762)=5+7+6+2=20
        

        Since 267 is the lowest possible n in sf(n) to obtain 20, g(20) is 267.

        • + 0 comments

          why we need to take 20?

    • + 1 comment

      what is the logic to find g(i)

      • + 1 comment

        test values in sf() from 1 until you get it !

        • + 1 comment

          you are talking about getsum()?

          • + 0 comments

            im talking about his question how to find g(i),

    • + 0 comments

      bro how to get these values can you help me out

    • + 0 comments

      Some G(i) values are Here :-) G[49] = 133356888899 G[50] = 12245677888899 G[51] = 34446666888899 G[52] = 134446666888899 G[53] = 12245578899999999 G[54] = 123345578899999999 G[55] = 1333666799999999999 G[56] = 12245556666799999999999 G[57] = 123345556666799999999999 G[58] = 1333579999999999999999999 G[59] = 122456679999999999999999999999 G[60] = 1233456679999999999999999999999 G[61] = 13444667779999999999999999999999

    • + 0 comments

      how we are getting these g values ? PLz explain

    • + 1 comment

      I have implemented two different algorithms (Java/Python) and I am implementing Dynamic Programming for optimization in both of them, but it seems that's not enough for this type of problem. Any one who has implemented other method with good results?

      • + 2 comments

        Baby steps :) This one is really tough. I have been dealing with this problem for almost a week and still I have not figured it out. After doing some research on DP, math induction, I was able to pass test #1 (brute force only allows you to pass test #0). Now my algorithm can calculate in fractions of secs up to g(74). I am suspecting that test #2+ are numbers where g(100)+. Any idea on how to predict?

        • + 0 comments

          check this one out def fact(a): if a==0: return 1 else: return a*fact(a-1) def g(i):
          n=1 h=0 while(i!=h): sum1=0 sum2=0 k=str(n) for u in k: sum1=sum1+fact(int(u))
          e=str(sum1) for j in e: sum2=sum2+int(j)
          h=sum2 n=n+1 else: return (n-1) def res(h,m):
          q=[] d=[] for i in range(1,h+1): sum11=0 t=str(g(i)) if(len(t)==1): d.append(int(t)) else: for i in t: sum11=sum11+int(i) d.append(sum11)
          print(sum(d)%m) x1=int(input()) l=[] for i in range(x1): l.append([int(x) for x in input().split()]) for i in range(len(l)): res(l[i][0],l[i][1])

        • + 0 comments

          I have a method that seems to correctly calculate g(n) for n > 63 very quickly (g(64) -> g(1000) in 0.005 sec) but for some reason from g(1) to g(63) its mostly incorrect.

          However, it seems like not even this method can work within the required time constraint for tests other than #0 and #1. I believe the numbers are not much bigger than g(150), but you must calculate lots of them, since q can get up to 10^5.

          I also implemented a semi-brute force method with DP, but I can only get through test #0.

    • + 0 comments

      g[12] =33 3! + 3! => 6+6 = 12 cant this be true

    • + 1 comment

      Your values from 41 are wrong!

      • + 0 comments
        G[42] =    344479;
          42  = sf(344479)
          42  = s(3! + 4! + 4! + 4! +   7! +     9!)
          42  = s( 6 + 24 + 24 + 24 + 5040 + 362880)
          42  = s(367998)
          42  = 3 + 6 + 7 + 9 + 9 + 8
        

        There are no number n smaller than 344479 which gives sf(n)=42.

    • + 0 comments

      try g(0) its gives you value 0.

    • + 1 comment

      value should be minimal right so for g[12]=33 3!=6 => 6+6=12

      • + 0 comments

        sf(44) = s(4!+4!) = s(24+24) = s(48) = 4 + 8 = 12
        sf(33) = s(3!+3!) = s( 6+ 6) = s(12) = 1 + 2 = 3 This is not 12.

    • + 0 comments

      why g[12] cannot be (33 or 34)and g[18] cannot be 66 and g[14] cannot be (233or 234) and g[13] cannot be (133 or 134)and g[19] cannot be 166 and g[20] cannot be 266, while g[12] can be 44 and g[13] can be 144.

    • + 0 comments

      g[20] can be 266 also and which is smaller than 267

    • + 1 comment

      for G[12] why cant we use 33 as 3!+3!=12

      • + 0 comments
         f(33) = 3! + 3! = 12
        sf(33) = s(12) = 1+2 = 3
        

        g[3] already has 5 as a lower value than 33.

    • + 0 comments

      like g(41) is 2355679, my code has to run 2355679 loops to find g41, it will eventually find it, but code runs out of time, how to deal with it.

    • + 0 comments

      what is the cindition of g[i]?? how are we calculating it?

    • + 1 comment

      Note that, the values of G(i) are not in "increasing" order but in "non decreasing" order.

      • + 1 comment

        please enlighten me with the difference

        • + 0 comments

          Many indeed make a mistake here. A non decreasing series is the one where any a(i)>=a(j), for all i>j. While on the other hand, if the series is said to be increasing then, it should strictly increase, which means a(i)>a(j), for all i>j. (Here a(m) denotes the mth term of the series, and i,j,m belong to positive integers). Thus, a series like 1,2,2,3,4,5,5,5,6,7,... must not be accounted as increasing but rather, non decreasing, as the terms never decrease but may remain the same. I do not know how include LaTeX here in HackerRank, but I think you will get that at least.. Hope this helps!

    • + 1 comment
      g(49) =  133356888899
      g(50) =  23345677888899
      g(51) =  34446666888899
      g(52) =  134446666888899
      

      I wish there was a solution posted somewhere.

      • + 0 comments

        ill try it tomorrow

    • + 0 comments

      Why G(18) = 67? Shouldnt it be 66?