Sum of the factorial of the digits of a number - wierd behaviour

Discussion in 'Python' started by SiWi, Dec 9, 2009.

  1. SiWi

    SiWi Guest

    Dear python community,
    I've got a wierd problem and I hope you can help me out at it.
    I wrote the following code to find the Sum of the factorial of the
    digits of a number (this is for Project Euler 74):

    def fac(n):
    x=1
    for i in range(2,n+1):
    x*=i
    return x

    t=tuple(fac(n) for n in range(1,10))

    def decimals(x):
    i=1
    d=[]
    while x>0:
    d.append(x%10)
    x=x/10
    return d

    def sumfac(x):
    return sum(t[n-1] for n in decimals(x))

    The problem is that i get the following results, for which I can't see
    any reason:
    sumfac(145)->145 (1!+4!+5!=1 + 24 +120 = 145) - ok
    sumfac(1454)-> 169 - ok
    sumfac(45362) -> 872 - ok
    sumfac(363600) -> 727212 - wrong, should be1454

    Greetings,
    SiWi.
     
    SiWi, Dec 9, 2009
    #1
    1. Advertising

  2. SiWi

    SiWi Guest

    On Dec 9, 6:36 pm, SiWi <> wrote:
    > Dear python community,
    > I've got a wierd problem and I hope you can help me out at it.
    > I wrote the following code to find the Sum of the factorial of the
    > digits of a number (this is for Project Euler 74):
    >
    > def fac(n):
    >     x=1
    >     for i in range(2,n+1):
    >         x*=i
    >     return x
    >
    > t=tuple(fac(n) for n in range(1,10))
    >
    > def decimals(x):
    >     i=1
    >     d=[]
    >     while x>0:
    >         d.append(x%10)
    >         x=x/10
    >     return d
    >
    > def sumfac(x):
    >     return sum(t[n-1] for n in decimals(x))
    >
    > The problem is that i get the following results, for which I can't see
    > any reason:
    > sumfac(145)->145 (1!+4!+5!=1 + 24 +120 = 145) - ok
    > sumfac(1454)-> 169 - ok
    > sumfac(45362) -> 872 - ok
    > sumfac(363600) -> 727212 - wrong, should be1454
    >
    > Greetings,
    > SiWi.


    Oops, found it myself. You can ignote the message above.
     
    SiWi, Dec 9, 2009
    #2
    1. Advertising

  3. SiWi

    Jon Clements Guest

    Even though you've worked it out -- a couple of tips:

    On Dec 9, 5:39 pm, SiWi <> wrote:
    > On Dec 9, 6:36 pm, SiWi <> wrote:
    >
    >
    >
    > > Dear python community,
    > > I've got a wierd problem and I hope you can help me out at it.
    > > I wrote the following code to find the Sum of the factorial of the
    > > digits of a number (this is for Project Euler 74):

    >
    > > def fac(n):
    > >     x=1
    > >     for i in range(2,n+1):
    > >         x*=i
    > >     return x

    >


    numpy/scipy etc... are quite useful for Euler :)

    They contain a function to do factorials (and loads more).

    This to one of the readable uses of 'reduce':
    def fac(n):
    reduce(operator.mul, xrange(2, n+1), n)

    > > t=tuple(fac(n) for n in range(1,10))

    >
    > > def decimals(x):
    > >     i=1
    > >     d=[]
    > >     while x>0:
    > >         d.append(x%10)
    > >         x=x/10
    > >     return d


    The builtin str object can take integers and return it as a string.


    def decimals(x):
    return map(int, str(x))
    decimals(145) == [1, 4, 5]

    >
    > > def sumfac(x):
    > >     return sum(t[n-1] for n in decimals(x))

    >


    And join the two:
    print sum(fac(n) for n in decimals(145))



    > > The problem is that i get the following results, for which I can't see
    > > any reason:
    > > sumfac(145)->145 (1!+4!+5!=1 + 24 +120 = 145) - ok
    > > sumfac(1454)-> 169 - ok
    > > sumfac(45362) -> 872 - ok
    > > sumfac(363600) -> 727212 - wrong, should be1454

    >
    > > Greetings,
    > > SiWi.

    >
    > Oops, found it myself. You can ignote the message above.


    You might also find it useful to write generators (esp. for primes and
    factorials).

    Cheers,

    Jon.
     
    Jon Clements, Dec 9, 2009
    #3
  4. > numpy/scipy etc... are quite useful for Euler :)

    I've come to love sympy, personally.

    > They contain a function to do factorials (and loads more).


    >>> from math import factorial
    >>> factorial(5)

    120

    Geremy Condra
     
    geremy condra, Dec 9, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shreyas Kulkarni
    Replies:
    1
    Views:
    978
    Gregory Toomey
    Dec 27, 2004
  2. Replies:
    59
    Views:
    1,761
    Richard Heathfield
    Sep 21, 2006
  3. bejiz
    Replies:
    12
    Views:
    630
  4. Chris Rebert
    Replies:
    1
    Views:
    409
    Grant Edwards
    Nov 5, 2010
  5. Tim Chase
    Replies:
    2
    Views:
    587
    Tim Chase
    Nov 6, 2010
Loading...

Share This Page