File: fib.l

package info (click to toggle)
euslisp 9.27%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 55,344 kB
  • sloc: ansic: 41,162; lisp: 3,339; makefile: 256; sh: 208; asm: 138; python: 53
file content (79 lines) | stat: -rw-r--r-- 1,494 bytes parent folder | download | duplicates (3)
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
78
79
(defun fact (n)
   (if (<= n 1) 1 (* (fact (1- n)) n)))

(defun fib (n)
   (if (< n 2)
       n
       (+ (fib (1- n)) (fib (- n 2)))))


(defun ifib (n)
   (declare (fixnum n))
   (if (< n 2)
	n
       (+ (the integer (ifib (1- n))) (the integer (ifib (- n 2))))))

(defun ffib (n)
   (declare (fixnum n))
   (if (< n 2)
	n
       (+ (the fixnum (ffib (1- n))) (the fixnum (ffib (- n 2))))))

(defun xfib (n0 n1 end)
   (let ((a (make-array (1+ end) :initial-element 0)))
      (setf (aref a 0) n0)
      (setf (aref a 1) n1)
      (do ((i 2 (1+ i)))
	  ((> i end))
	(setf (aref a i) (+ (aref a (1- i)) (aref a (- i 2)))))
      a))

(defvar fibm (make-array 1000))

(defun mfib (n)
   (if (aref fibm n)
	(aref fibm n)
	(if (< n 2) 
	    (setf (aref fibm n) 1)
	    (setf (aref fibm n) (+ (mfib (1- n)) (mfib (- n 2)))))))

	
(defvar fiblist nil)

(defun afib (n)
   (if (assoc n fiblist)
	(second (assoc n fiblist))
	(if (< n 2) 
	    1
	    (progn
		(push  (list n (+ (afib (1- n)) (afib (- n 2)))) fiblist)
		(cadar fiblist)) ) ) )

(defun etafib (n)
   (if (< n 2)
       n
       (+ (etafib (sub1 n)) (etafib (- n 2)))))


(defun gcd (a b)
    (if (= b 0)
	a
	(gcd b (mod a b))))

(defun factor (x &optional (y 2))
   (if (<= x y)
       (list x)
       (if (= (mod x y) 0)
	   (cons y (factor (/ x y) y) )
	   (factor x (1+ y) ) ) ) )

(defun power (a n)
   (if (= n 0)
       1
       (let ((b (power a (/ n 2))))
	  (setq b (* b b))
	  (if (= (mod n 2) 1) (setq b (* b a)))
	  b)))