Check Prime/Prime Generation -LISP

Program:

(defun gen(a b)
(if (<= a 1) (setf a 2))
(do ((i a (+ i 1) )) ((= i b))
    (prm i)
    ))
(defun prm( num)
    (setf flag 1) (print num)
    (do ((i 2 (+ i 1) )) ((= i  num))
        (if (= 0 (mod num i)) (setf flag 2)
            )
    )
    (if (= flag 1) (format t "prime") (format t "not prime"))
)

Output:

Break 13 [15]> (load 'pr2.lsp)
;; Loading file pr2.lsp ...
;; Loaded file pr2.lsp
T
Break 13 [15]> (gen 1 20)

2 prime
3 prime
4 not prime
5 prime
6 not prime
7 prime
8 not prime
9 not prime
10 not prime
11 prime
12 not prime
13 prime
14 not prime
15 not prime
16 not prime
17 prime
18 not prime
19 prime
NIL
Break 13 [15]>

Download Runnable Code

1 comment:

Related Posts Plugin for WordPress, Blogger...