Project Euler

Project Euler Problem 20

steloflute 2012. 6. 3. 22:58

Problem 20

21 June 2002

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!


Answer:
648

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace Euler {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine(Enumerable.Range(1, 100)
                .Select(x => new BigInteger(x))
                .Aggregate(BigInteger.Multiply).ToString().Select(x => x - '0').Sum());
            Console.ReadKey();
        }
    }
}


Racket


(define (char->digit x)
  (- (char->integer x)
     (char->integer #\0)))
(define (sum-digits x)
  (apply + (map char->digit (string->list (number->string x)))))
(define (problem20)
  (display (sum-digits (apply * (range 2 100 1)))))
(problem20)



'Project Euler' 카테고리의 다른 글

Project Euler Problem 22  (0) 2012.06.03
Project Euler Problem 21  (0) 2012.06.03
Project Euler Problem 19  (0) 2012.06.03
Project Euler Problem 18  (0) 2012.06.03
Project Euler Problem 17  (0) 2012.06.03