Project Euler #020 – 100の階乗

In: Project Euler

14 1月 2010

Project EulerをClojureでおさらいシリーズ。
Problem 20

オリジナルはこちら

日本語訳:

n! は n × (n-1) × ・・・ × 3 × 2 × 1 を表す。

1000! の各桁の数字の和を求めよ。

瞬殺の予感。

以前Rubyで解いたもの:

#!/usr/bin/env ruby
 
p 1.upto(100).inject(:*).to_s.split('').inject(0){|s,x| s + x.to_i}

Rubyステキ。

Clojureでは:

(ns euler.p020)
 
(defn fact
  "Return factorial of N."
  [n]
  (apply * (range 1 (inc n))))
 
(defn sum-of-digits
  "Return the sum of each digits of N."
  [n]
  (apply + (map #(Integer. (str %)) (str n))))
 
(println "Answer : " (sum-of-digits (fact 100)))

ほとんどいままでの使い回し。

blog comments powered by Disqus
Get Adobe Flash playerPlugin by wpburn.com wordpress themes

About this blog

私 manjilab のポータル的サイトになっております。日々気付いたこと、考えたこと、発表したいものを載せていきます。

Photostream

    Lisp indent 考察Lisp indent 考察Lisp indent 考察Lisp indent 考察Lisp indent 考察Tips to use Clojure(Lisp) with TextMateTips to use Clojure(Lisp) with TextMateTips to use Clojure(Lisp) with TextMate