## Lisp

Ekim 26th, 2011 § Yorum yok

```(loop for x from 1 to 3
do (loop for y from 11 to 12
do (print (+ x y)) ))

(defun factors (num)
"This function will find the factors of the argument num."
(let ((l '()))
(do ((current 1 (1+ current)))
((> current (/ num current)))
(if (= 0 (mod num current))
(if (= current (/ num current))
(setf l (append l (list current)))
(setf l (append l (list current (/ num current)))))))
(sort l #'< )))

(defun coprimesp (num1 num2)
"Returns t if numbers are coprimes, nil otherwise."
(let ((flag t))
(dolist (atom1 (rest (factors num1)))
(dolist (atom2 (rest (factors num2)))
(if (= atom1 atom2)
(setf flag nil))))
flag))

(defun find-primes (top)
"Finds primes starting at one and searching up to given number, top."
(let ((l '()))
(dotimes (x top)
(if (= 2 (length (factors x)))
(setf l (append l (list x)))))
l))

(defun prime-factors (num)
(let ( (f (factor num)) (l '()) )
(dolist (element f)
(if (primep element)
(setf l (append l (list element)))
(setf l (append l (prime-factors element)))))
l))

(defun prime-p (n)
(declare ((speed 3) (safety 0) (fixnum-safety 0))
(fixnum n))
(cond
((and (<= n 11) (member n '(2 3 5 7 11)) t)
((= (rem n 2) 0) nil)
((= (rem n 3) 0) nil)
((= (rem n 5) 0) nil)
((= (rem n 7) 0) nil)
(t
(loop for i fixnum from 11 to (isqrt n) by 2
(when (= (rem n i) 0) (return-from prime-p nil))
t)))

(defun sieve5 (n)
"Returns a list of all primes from 2 to n"
(declare (fixnum n) (optimize (speed 3) (safety 0)))
(let* ((a (make-array n :element-type 'bit :initial-element 0))
(result (list 2))
(root (isqrt n)))
(declare (fixnum root))
(do ((i 3 (the fixnum (+ i 2))))
((>= i n) (nreverse result))
(declare (fixnum i))
(progn (when (= (sbit a i) 0)
(push i result)
(if (<= i root)
(do* ((inc (+ i i))
(j (* i i) (the fixnum (+ j inc))))
((>= j n))
(declare (fixnum j inc))
(setf (sbit a j) 1))))))))

(defun divides? (a b)
(= (mod b a) 0))

(defun find-divisor (n test-divisor)
(cond ((> (* test-divisor test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(t (find-divisor n (+ test-divisor 1))))))

(defun smallest-divisor (n)
(find-divisor n 2))

(defun prime? (n)
(= n (smallest-divisor n)))

=== Parametre olan Fonksiyon isimleri===

(defun sum (term a next b)
(if (> a b)
0
(+ (funcall term a) (sum term (funcall next a) next b))))

(defun sum-int (a b)
(defun myidentity (a) a)
(sum #'myidentity a #'1+ b))

veya

(defun sum-int (a b)
(sum (lambda(x) x) a #'1+ b))

(defun sum-sqr (a b)
(defun square (a) (* a a))
(sum #'square a #'1+ b))

(defun mysqrt(x) (fixed-point (lambda(y) (average (/ x y) y)) 1))

(defun sum-of-digits(n)
(if (= (abs n) 0) 0 (+ (mod (abs n) 10) (sum-of-digits (/ (- (abs n)  (mod (abs n) 10)) 10)))))

(defun factorial(n) (if (= n 0) 1 (* n (factorial (- n 1)))))

(reduce '+ (find-primes 2000000))

```

### Ne yapıyorum ben!?

Lisp başlıklı yazıyı okuyorsun.