Advertisement
dgulczynski

MP lab 0

Feb 21st, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 1.25 KB | None | 0 0
  1. #lang racket
  2. (define (bigger-sum a b c)
  3.   (cond ((and (<= a b) (<= a c)) (+ (* b b) (* c c)))
  4.         ((and (<= b a) (<= b c)) (+ (* a a) (* c c)))
  5.         (else (+ (* a a) (* b b)))))
  6.  
  7. (define (power-close-to b n)
  8.   (define (calc e )
  9.     (if (> (expt b e) n) e (calc (+ e 1) )))
  10.  
  11.   (cond [(< b 1) (if (>= n 1) false (calc 0))]
  12.         [else (calc 0)]))
  13.  
  14. (define (square x)
  15.   (* x x))
  16. (define (dist x y)
  17.   (abs (- x y)))
  18. (define (cube x)
  19.   (* x x x))
  20.  
  21. (define (cube-root x)
  22.   ;; lokalne definicje
  23.   ;; poprawienie przybliżenia pierwiastka z x
  24.   (define (improve approx)
  25.     (/ (+ (/ x (square approx))  (* 2 approx)) 3))
  26.   ;; nazwy predykatów zwyczajowo kończymy znakiem zapytania
  27.   (define (good-enough? approx)
  28.     (< (dist x (cube approx)) 0.0001))
  29.  
  30.   ;; główna procedura znajdująca rozwiązanie
  31.   (define (iter approx)
  32.     (cond
  33.       [(good-enough? approx) approx]
  34.       [else                  (iter (improve approx))]))
  35.  
  36.   (iter 1.0))
  37.  
  38. (define (test-cube-root x)
  39.   (display "Pierwiastek z ")
  40.   (display x)
  41.   (display ":\ncube-root:\t")
  42.   (display (cube-root x))
  43.   (display "\nexpt 1/3:\t")
  44.   (display (expt x (/ 1 3)))
  45.   (display "\nroznica:\t")
  46.   (display (abs (- (expt x (/ 1 3)) (cube-root x))))
  47.   (display "\n"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement