Clojure Euler: Problem 013

Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.


Assume, here is a huge amount of digits. In any case, you can always find them in permalink.

What is the problem to sum one-hundred numbers?

(reduce + numbers)


Are you kidding me? Not so fast. The question is what type of number you must choose to sum all these numbers. int? long? looooong? super long long?

Technique to sum very long numbers called Bignum arithmetic. To solve that problem you just need to implement this technique… or use already implemented.

Java (yeah we talking about it) support long arithmetics with BigInteger. And clojure does.

(reduce + (map bigint numbers))

With that function you’ll get the big number. Just truncate first 10 digits and you are good.

(apply str (take 10 (str (reduce + (map bigint numbers)))))

(for [i “GitHub”] i)

P.S. Ideally, last line should be wrapped with (read-string ...) to produce number type. But we using manual submission, so it is fair.

mishadoff 25 January 2013
blog comments powered by Disqus