-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.lisp
40 lines (32 loc) · 964 Bytes
/
util.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
;;;; utilities
(defun square (x)
"Returns the square of x."
(* x x))
(defun prime-factors (num)
"Returns prime factors of number."
(assert (integerp num)
(num))
(remove-if #'zerop
(loop for p in (prime-numbers-upto num)
collecting (if (zerop (mod num p))
p
0))))
(defun prime-numbers-upto (limit)
"Returns prime numbers less than or equal to limit."
(assert (integerp limit)
(limit))
(remove-if #'zerop
(loop for i from 1 upto limit
collecting (if (primep i)
i
0))))
(defun primep (n)
"Returns true for prime numbers, otherwise nil."
(assert (integerp n)
(n))
(cond ((< n 2) nil)
((= n 2) t)
((evenp n) nil)
(t
(loop for i from 3 upto (sqrt n) by 2
never (zerop (mod n i))))))