-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_64.clj
More file actions
43 lines (36 loc) · 789 Bytes
/
problem_64.clj
File metadata and controls
43 lines (36 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
(ns clj-test.core)
(use 'clojure.contrib.math)
(def d (memoize (fn [S n]
(if (zero? n)
1
(/
(- S (expt (m S n) 2))
(d S ( dec n)))))))
(def m (memoize (fn [S n]
(if (zero? n)
0
(-
(* (d S (dec n)) (a S (dec n)))
(m S (dec n)))))))
(def a (memoize (fn [S n]
(int
(if (zero? n)
(sqrt S)
(/
(+ (a S 0) (m S n))
(d S n)))))))
(defn find-period-length
([S] (find-period-length S {} 0))
([S res n]
(let [t [(a S n) (m S n) (d S n)]]
(if (res t)
(dec n)
(recur S
(assoc res t true)
(inc n))))))
(defn irrational? [n]
(not (integer? (sqrt n))))
(count
(filter odd?
(map find-period-length
(filter irrational? (range 2 10000)))))