-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuler037.lisp
59 lines (52 loc) · 1.19 KB
/
euler037.lisp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
;;;;
;;;; Project Euler - Problem 37
;;;;
(load "prime.lisp")
(defun euler037 ()
(prime-init 1000000)
(let ((iter 5)
(number-found 0)
(sum 0))
(loop
(when (= number-found 11)
(return sum))
(let ((prime-to-check (prime iter)))
(if (and (check-left prime-to-check)
(check-right prime-to-check))
(progn
(setf sum (+ sum prime-to-check))
(incf number-found)))
(incf iter)))))
(defun check-left (n)
(if (null n)
t
(if (primep n)
(check-left (truncate-left n))
nil)))
(defun truncate-left (n)
(if (< n 10)
nil
(let* ((log (log10 n))
(floor-value (floor n log))
(left (- n (* floor-value log))))
left)))
(defun check-right (n)
(if (null n)
t
(if (primep n)
(check-right (truncate-right n))
nil)))
(defun truncate-right (n)
(if (< n 10)
nil
(floor n 10)))
(defun log10 (n)
(cond
((> n 10000000) 10000000)
((> n 1000000) 1000000)
((> n 100000) 100000)
((> n 10000) 10000)
((> n 1000) 1000)
((> n 100) 100)
((> n 10) 10)
(t 1)))