Skip to content

Instantly share code, notes, and snippets.

@emanon001
Created January 6, 2013 08:56
Show Gist options
  • Save emanon001/4466138 to your computer and use it in GitHub Desktop.
Save emanon001/4466138 to your computer and use it in GitHub Desktop.
Project Euler Problem 20 #mitori_clj
;;; 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"
@tnoda
Copy link

tnoda commented Jan 9, 2013

👍 あまり突っ込むところが無いので,逆に困ってしまいます.

@ypsilon-takai
Copy link

僕もぜんぜん突っ込むところが無いです。

問題の意図は多倍長整数を扱うということだと思うので、何かそれ関連の話題を考えたのですが、思いつきません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment