Euler Solution 16

From ProgSoc Wiki

Jump to: navigation, search

Contents

Solutions for Problem 16

What is the sum of the digits of the number 21000?

Lisp by SanguineV

Runtime (script mode): 43.7ms

(define (sumdigit n)
  (if (> n 9)
  ((lambda (rem) (+ rem (sumdigit (/ (- n rem) 10)))) (modulo n 10))
  n))

(begin (display (sumdigit (expt 2 1000))) (newline))
(exit)

Python by Althalus

Runtime: 1 ms

num = 2**1000
num_split = [c for c in str(num)]
total = 0
for num in num_split:
        total+= int(num)

print(total)

Ruby by ctd

Runtime: 12ms

def sumOfDigits(num)
	if num < 10
		return num
	else
		return (num % 10) + sumOfDigits(num / 10)
	end
end
puts sumOfDigits(2**1000);
Personal tools