Skip to content

Commit

Permalink
add test for 1.8
Browse files Browse the repository at this point in the history
  • Loading branch information
burenkov-anton committed Feb 2, 2021
1 parent 948ee80 commit 8454c63
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resources/lang/ru/exercises/1_8.php
Expand Up @@ -7,7 +7,7 @@
"Метод Ньютона для кубических корней основан на том, что если y является приближением к кубическому корню из x, " .
"то мы можем получить лучшее приближение по формуле",
'2' =>
"С помощью этой формулы напишите процедуру вычисления кубического корня, подобную процедуре для квадратного корня. " .
"С помощью этой формулы напишите процедуру вычисления кубического корня cube-root, подобную процедуре для квадратного корня. " .
"(В разделе 1.3.4 мы увидим, что можно реализовать общий метод Ньютона как абстракцию этих процедур для квадратного и кубического корня.)",
],
];
11 changes: 11 additions & 0 deletions resources/views/exercise/solution_stub/1_8.blade.php
@@ -0,0 +1,11 @@
#lang racket/base
(require rackunit)

;;; BEGIN
{!! $solution !!}
;;; END

(check-equal? (round (* 1000 (cube-root 8.0))) 2000.0)
(check-equal? (round (* 1000 (cube-root 1000.0))) 10000.0)
(check-equal? (round (* 1000 (cube-root 1000000000.0))) 1000000.0)
(check-equal? (round (* 1000 (cube-root 0.008))) 200.0)
17 changes: 17 additions & 0 deletions resources/views/exercise/solution_stub/1_8_solution.blade.php
@@ -0,0 +1,17 @@
(define (sqr x) (* x x))

(define (cube x) (* x x x))

(define (improve guess x)
(/ (+ (* 2 guess) (/ x (sqr guess))) 3))

(define (good-enough? guess x)
(< (abs (- (cube guess) x)) 0.000001))

(define (cube-root-iter guess x)
(if (good-enough? guess x)
guess
(cube-root-iter (improve guess x) x)))

(define (cube-root x)
(cube-root-iter 1.0 x))

0 comments on commit 8454c63

Please sign in to comment.