Sum of Factorial's digit

Level pending

n! = n × (n − 1) × ... × 3 × 2 × 1

For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

The sum of the digits in the number 2013! = X.

Find the last three digits of 'X'.


The answer is 21.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

4 solutions

Lokesh Sharma
Dec 16, 2013

Python Solution:

import math

print sum(map(int, list(str(math.factorial(2013)))))

Nice one liner!You don't need the list function though, map automatically changes a string object to a list.

print sum(map(int, str(math.factorial(2013))))

Thaddeus Abiy - 7 years, 5 months ago

Log in to reply

Thanks for the info mate.

Lokesh Sharma - 7 years, 5 months ago
Jubayer Nirjhor
Dec 17, 2013

Python Solution

from math import factorial

def digit_sum(n):
    sum_ = 0
    while n:
        sum_ += n % 10
        n /= 10
    return sum_

A = factorial(2013)

print digit_sum(A), digit_sum(A) % 1000

Output: 24021 , 21 24021, \fbox{21}

Mayank Shreshtha
Dec 18, 2013

import sys sys.setrecursionlimit(5000) def fact(n): if n == 0 : return 1 else: return n*fact(n-1)

def sum_digits(n): s = 0 while n: s += n % 10 n /= 10 return s

m = fact(2013)

print sum_digits(m)

In the start i have to increase the recursion limit as if we run the code normally there will be run time error. please correct the indentation as i am unable to write the code in a formatted manner.

Manoj Pandey
Dec 16, 2013

Python Implementation:

def factorial(n): 
  a=2 
  fact=1 
  while a<=n: 
    fact=fact*a 
    a=a+1 
  return fact

def sum_digits(n): 
  string=str(n) 
  a=0 
  sum=0 
  while a<len(string): 
    sum=sum+int(string[a]) 
    a=a+1 
  return sum 

x=sum_digits(factorial(2013)) 

print x

Result= 24 021 24\boxed{021}

Can anyone solve this without using calculating devices? With your bare mind only?

Master Mind - 7 years, 3 months ago

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...