Skip to content

Instantly share code, notes, and snippets.

@ponkore
Created April 22, 2012 13:17
Show Gist options
  • Save ponkore/2464090 to your computer and use it in GitHub Desktop.
Save ponkore/2464090 to your computer and use it in GitHub Desktop.
Project Euler Problem 1
;;; Project Euler Problem 1
;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%201
;;;
;;; 10未満の自然数のうち、3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり、 これらの合計は 23 になる。
;;; 同じようにして、1,000 未満の 3 か 5 の倍数になっている数字の合計を求めよ。
;;; 3 もしくは 5の倍数の場合 true、そうでない場合 false を返す filter 判定用関数
(defn is-3-or-5-mul? [n] (or (= (mod n 3) 0) (= (mod n 5) 0)))
;;; テスト
;;; (filter is-3-or-5-mul? (take 40 (iterate inc 1)))
;;; => (3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40)
;;; 課題の答えは、1から1,000 までの数列から、is-3-or-5-mul? が true になる数値を抽出し、足し算する。
(apply + (filter is-3-or-5-mul? (take (dec 1000) (iterate inc 1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment