Nuparu00

fibififibfifbifbifbibf

Oct 18th, 2021
610
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;Lecture 2
  2.  
  3. (defun hypotenuse (x y) (sqrt (+ (* x x) (* y y))))
  4.  
  5. (defun circle-area (r) (* pi r r))
  6.  
  7. (defun cone-volume (r h) (* 1/3 h (circle-area r)))
  8.  
  9. (defun signumb (x)
  10.   (if (> x 0) 1
  11.     (if (= x 0 ) 0 -1)))
  12.  
  13. (defun mini (x y)
  14.   (if (< x y) x y))
  15.  
  16. (defun maxi (x y)
  17.   (if (> x y) x y))
  18.  
  19. (defun point-dst (A-x A-y B-x B-y)
  20.   (let ((x (- A-x B-x)) (y (- A-y B-y)))
  21.     (hypotenuse x y)))
  22.  
  23. (defun trianglep (a b c)
  24.   (and (tri-side-p a b c) (tri-side-p b c a) (tri-side-p c a b))
  25.   )
  26.  
  27. (defun tri-side-p (a b c)
  28.   (> (+ a b) c))
  29.  
  30. (defun heron (a b c)
  31.   (let ((s (/ (+ a b c) 2)))
  32.     (sqrt (* s (- s a) (- s b) (- s c)))))
  33.  
  34. (defun heron-cart(A-x A-y B-x B-y C-x C-y)
  35.   (heron (point-dst A-x A-y B-x B-y) (point-dst B-x B-y C-x C-y) (point-dst C-x C-y A-x A-y)))
  36.  
  37.  
  38. ;;Lecture 3
  39.  
  40. (defun get-gcd (a b)
  41.   (if (= b 0) a (get-gcd b (mod a b))))
  42.  
  43.  
  44.  
  45. (defun digit-count (x) (digit-count-internal x 0))
  46.  
  47. (defun digit-count-internal (x count)
  48.        (if (< x 1) count (digit-count-internal (/ x 10) (+ count 1))))
  49.  
  50. ;;Returns the digit at Nth position from the right, starts from 1
  51. (defun digit (x n)
  52.         (floor (* (- (/ x (power 10 n))
  53.             (floor (/ x (power 10 n))) ) 10)) )
  54.  
  55. (defun power (x n) (power-internal x n 1))
  56.  
  57. (defun power-internal (x n res) (if (> n  0) (power-internal x (- n 1) (* res x)) res))
  58.  
  59. (defun digit-sum (x) (digit-sum-internal x (digit-count x) 1 0))
  60.  
  61. (defun digit-sum-internal (x length pos sum)
  62.   (if (> pos length) sum (digit-sum-internal x length (+ pos 1) (+ sum (digit x pos)))))
  63.  
  64. ;;Uses digit-sum --> see 5 lines above
  65. (defun multiply-of-9-internal-p (x) (if (>= x 10) (multiply-of-9-internal-p (digit-sum x)) (= x 9) ))
  66.  
  67. (defun multiply-of-9-p (x) (multiply-of-9-internal-p (digit-sum x)))
  68.  
  69. (defun ellipse-area (a b) (if (eql b t) (ellipse-area a a) (* pi a b)))
  70.  
  71. (defun heron-sqrt (a x dif)
  72.   (if (<= (abs-delta (power x 2) a) dif) (* x 1.0)
  73.       (heron-sqrt a (/ (+ x (/ a x)) 2) dif)  
  74. ))
  75.  
  76. (defun abs-delta (a b)
  77.   (abs (- a b)))
  78.  
  79. (defun leibniz (deviation)
  80.   (leibniz-internal (/ deviation 10) 1 -1 3))
  81.  
  82. (defun leibniz-internal (deviation prev m n)
  83.   (let ((fraction (/ m n)))
  84.     (if (<= (abs fraction) deviation) (* 4.0 prev)
  85.       (leibniz-internal deviation (+ prev fraction) (- m) (+ 2 n))))))
  86.  
  87. (defun range-sum-dumb (a b)
  88.   (if(> a b) 0
  89.     (if (= a b) a
  90.       (+ b (range-sum-dumb a (- b 1))))))
  91.  
  92. (defun range-sum (a b)
  93.   (if (> a b) 0 (range-sum-internal a b a)))
  94.  
  95. (defun range-sum-internal (a b prev)
  96.   (if (= a b) prev (range-sum-internal a (- b 1) (+ prev b))))
  97.  
  98. ;;Lecture 4
  99.  
  100. (defun power2 (n)
  101.   (* n n))
  102.  
  103. (defun fast-power (a n)
  104.   (fast-power-iter a n 1))
  105.        
  106. (defun fast-power-iter (a n ir)
  107.   (cond ((= n 0) ir)
  108.         ((evenp n)
  109.          (power2 (fast-power-iter a (/ n 2) ir)))
  110.         (t (fast-power-iter a (- n 1) (* a ir)))))
  111.  
  112. ;;Smaller first, larger second
  113. (defun dividesp (x y)
  114.   (= (mod y x) 0))
  115.  
  116. (defun remc (x y)
  117.   (- x (* y (truncate (/ x y)))))
  118.  
  119. (defun primep (x)
  120.   (prime-iter-p x (floor (/ x 2))))
  121.  
  122. (defun prime-iter-p (x check)
  123.   (or (<= check 1) (and (not (dividesp check x)) (prime-iter-p x (- check 1)))))
  124.  
  125. (defun perfectp (x)
  126.   (perfect-iter-p x (floor (/ x 2)) 0))
  127.  
  128. (defun perfect-iter-p (x check ir)
  129.   (or
  130.    (and (<= check 0) (= ir x))
  131.    (and (> check 0)
  132.         (if (dividesp check x)
  133.             (and (<= (+ check ir) x) (perfect-iter-p x (- check 1) (+ ir check)))
  134.           (perfect-iter-p x (- check 1) ir)))))
  135.  
  136. (defun pascal (row e)
  137.   (if (or (< e 0) (> e row) (and (= row 0) (not (= e 0))))
  138.       0
  139.     (if (= row 0) 1
  140.       (+ (pascal (- row 1) (- e 1)) (pascal (- row 1) e)))))
  141.  
  142. (defun fibbi (n)
  143.   (fibbi-iter n 0 1))
  144.  
  145. (defun fibbi-iter (n current next)
  146.   (if (<= n 0) current (fibbi-iter (- n 1) next (+ current next))))
  147.  
RAW Paste Data