View Single Post
Her er oppgave 20 (projecteuler.net)

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!

Jeg fant en løsning jeg lagde for en stund tilbake, dere som har startet å programmere i Python ,kan jo prøve å forstå hvordan jeg har gjort det.

Kode

import time

timer = 0
start = time.clock()
def num(n):
    if n==0:
        return 1
    else:
        return n * num(n-1)

print sum([int(i) for i in str(num(100))])
end = time.clock()
timer += end - start

print "time:", timer*1000, "ms"