Euler Problem 20

Posted by MarkPearl on Geeks with Blogs See other posts from Geeks with Blogs or by MarkPearl
Published on Fri, 17 Jun 2011 15:22:14 GMT Indexed on 2011/06/20 16:24 UTC
Read the original article Hit count: 200

Filed under:

 

This was probably one of the easiest ones to complete – a quick bash got me the following…

The Problem

n! means 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.

Find the sum of the digits in the number 100!

The Solution

 

private static BigInteger Factorial(int num)
{            
    if (num > 1) return (BigInteger)num * Factorial(num - 1);
    else return 1;
}

private static BigInteger SumDigits(string digits)
{
    BigInteger result = 0;
    foreach (char number in digits)
    {                
        result += Convert.ToInt32(number)-48;
    }
    return result;
}

static void Main(string[] args)
{            
    Console.WriteLine(SumDigits(Factorial(100).ToString()));
    Console.ReadLine();
}

© Geeks with Blogs or respective owner