Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- (define (bigger-sum a b c)
- (cond ((and (<= a b) (<= a c)) (+ (* b b) (* c c)))
- ((and (<= b a) (<= b c)) (+ (* a a) (* c c)))
- (else (+ (* a a) (* b b)))))
- (define (power-close-to b n)
- (define (calc e )
- (if (> (expt b e) n) e (calc (+ e 1) )))
- (cond [(< b 1) (if (>= n 1) false (calc 0))]
- [else (calc 0)]))
- (define (square x)
- (* x x))
- (define (dist x y)
- (abs (- x y)))
- (define (cube x)
- (* x x x))
- (define (cube-root x)
- ;; lokalne definicje
- ;; poprawienie przybliżenia pierwiastka z x
- (define (improve approx)
- (/ (+ (/ x (square approx)) (* 2 approx)) 3))
- ;; nazwy predykatów zwyczajowo kończymy znakiem zapytania
- (define (good-enough? approx)
- (< (dist x (cube approx)) 0.0001))
- ;; główna procedura znajdująca rozwiązanie
- (define (iter approx)
- (cond
- [(good-enough? approx) approx]
- [else (iter (improve approx))]))
- (iter 1.0))
- (define (test-cube-root x)
- (display "Pierwiastek z ")
- (display x)
- (display ":\ncube-root:\t")
- (display (cube-root x))
- (display "\nexpt 1/3:\t")
- (display (expt x (/ 1 3)))
- (display "\nroznica:\t")
- (display (abs (- (expt x (/ 1 3)) (cube-root x))))
- (display "\n"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement