Skip to content

Instantly share code, notes, and snippets.

@quephird
Last active December 10, 2015 22:46
Show Gist options
  • Save quephird/7aaaef31211735962e3f to your computer and use it in GitHub Desktop.
Save quephird/7aaaef31211735962e3f to your computer and use it in GitHub Desktop.
This is a solution for ProjectEuler #13 that avoids "cheating" by resorting to BigInteger.
(defn solution [big-string]
(->> big-string
(clojure.string/split-lines) ; Break string into individual numbers
(map reverse) ; Reverse the digits in each
(map (fn [n] (map #(Integer/parseInt (str %)) n))) ; Turn all characters into actual ints
(apply map +) ; Sum each column of digits
(reduce (fn [[digits carry] n] ; Fold over the partial sums,
[(conj digits (rem (+ n carry) 10)) ; adding the current sum modulo ten to the list of digits
(quot (+ n carry) 10)]) [() 0]) ; and taking the quotient over ten as the carry
((fn [[digits carry]] (str carry (apply str digits)))) ; Convert back to a single string, last carry at the front.
(take 10))) ; Take the first ten characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment