-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorization.lsp
executable file
·77 lines (73 loc) · 1.42 KB
/
factorization.lsp
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
; ========================================
; DETERMINE power of i can be divided by n
; ========================================
(defun level-count(n i result)
(if (= (mod n i) 0)
(level-count (/ n i) i (+ result 1))
result
)
)
; ========================================
; APPEND list of factorization
; ========================================
(defun add-element(i m result)
(nconc result (cons (cons i m) nil))
)
; ========================================
; CASE odd-factorization
; ========================================
(defun odd-fact(n jn result)
(cond
((> n 1)
(let
(
(jm (level-count n jn 0))
)
(cond
((> jm 0)
(odd-fact
(/ n (expt jn jm))
(+ jn 2)
(add-element jn jm result)
)
)
(T
(odd-fact n (+ jn 2) result)
)
)
)
)
(T
result
)
)
)
; ===================================================
; Using recursion to get the final factorization of N
; ===================================================
(defun fact-get(n result)
(let
(
(m (level-count n 2 0))
)
(cond
((> m 0)
(odd-fact
(/ n (expt 2 m))
3
(add-element 2 m result)
)
)
(T
(odd-fact n 3 result)
)
)
)
)
; ===========================
" FACTORIZATION "
; Author: Binh D. Nguyen
; ===========================
(defun f(n)
(fact-get n '())
)