Created
January 6, 2013 08:56
-
-
Save emanon001/4466138 to your computer and use it in GitHub Desktop.
Project Euler Problem 20 #mitori_clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; Project Euler Problem 20 | |
;;; 「100! の各桁の数字の和を求めよ」 | |
;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2020 | |
(ns emanon001.projecteuler-answers.problem-020 | |
(:require [clojure.test :refer [are]])) | |
(defn factorial | |
"n の階乗を返します" | |
[n] | |
(apply *' (range 1 (inc n)))) | |
(are [n expected] | |
(= (factorial n) expected) | |
0 1 | |
1 1 | |
10 3628800) | |
(defn sum-digits | |
"n の各桁の和を返します" | |
[n] | |
(->> n | |
str | |
(map #(Character/digit % 10)) | |
(apply +'))) | |
(are [n expected] | |
(= (sum-digits n) expected) | |
1 1 | |
12 3 | |
3628800 27) | |
(defn solve | |
"n の階乗の、各桁の和を返します" | |
([] (solve 100)) | |
([n] | |
(->> n | |
factorial | |
sum-digits))) | |
(are [n expected] | |
(= (solve n) expected) | |
1 1 | |
4 6 | |
10 27) | |
(time (solve)) | |
; "Elapsed time: 1.06 msecs" |
僕もぜんぜん突っ込むところが無いです。
問題の意図は多倍長整数を扱うということだと思うので、何かそれ関連の話題を考えたのですが、思いつきません。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍 あまり突っ込むところが無いので,逆に困ってしまいます.