Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 20 lines (17 sloc) 0.781 kb
b8ed113 Yasuhisa Yoshida なぜ動くか分からないけど、問題1.28が解けた...
syou6162 authored
1 (ns sicp-practice.ex-1-28
2 (:use [sicp-practice.ex-1-21]))
3
4 (defn expmod [base exp m]
5 (cond (= exp 0) 1
6 (even? exp) (let [n (expmod base (/ exp 2) m)
7 x (rem (square n) m)]
8 (if (and (not (= n 1)) (not (= n (- m 1))) (= x 1))
9 0 x))
10 :else (rem (* base (expmod base (- exp 1) m)) m)))
11
12 (defn miller-rabin-test [n]
13 (letfn [(try-it [n a] (= (expmod a (- n 1) n) 1))
14 (helper [n a] (cond (= a 0) true ;; fast-prime?のパクリ
15 (try-it n a) (try-it n (- a 1))
16 :else false))]
17 (helper n (dec n))))
18
19 (map #(vector % (miller-rabin-test %)) (range 3 20))
20 ; ([3 true] [4 false] [5 true] [6 false] [7 true] [8 false] [9 false] [10 false] [11 true] [12 false] [13 true] [14 false] [15 false] [16 false] [17 true] [18 false] [19 true])
Something went wrong with that request. Please try again.