-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sqrt and cbrt (cubic roots finder) functions
- Loading branch information
0 parents
commit 137dcaf
Showing
5 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# SICP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#lang racket/base | ||
|
||
(require rackunit "cbrt.rkt") | ||
|
||
(check-equal? (round (cbrt 27)) 3.0 "Is cubic root of 27 is 3?") | ||
(check-equal? (round (cbrt 1000)) 10.0 "Is cubic root of 1000 is 10?") | ||
(check-equal? (round (cbrt 848471250563)) 9467.0 "Is square root of 848471250563 is 9467?") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#lang racket | ||
|
||
(define (cbrt x) | ||
(define (square n) (* n n)) | ||
(define (improve guess) | ||
(/ (+ (* guess 2) (/ x (square guess))) 3)) | ||
(define (good-enough? previous-guess guess) | ||
(< (abs (/ (- guess previous-guess) guess)) 0.00000000001)) | ||
(define (cbrt-iter guess) | ||
(if (good-enough? guess (improve guess)) | ||
guess | ||
(cbrt-iter (improve guess)))) | ||
(cbrt-iter 1.0)) | ||
|
||
(provide cbrt) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#lang racket/base | ||
|
||
(require rackunit "sqrt.rkt") | ||
|
||
(check-equal? (round (sqrt 81)) 9.0 "Is square root of 81 is 9?") | ||
(check-equal? (round (sqrt 4096)) 64.0 "Is square root of 4096 is 64?") | ||
(check-equal? (round (sqrt 9801)) 99.0 "Is square root of 9801 is 99?") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#lang racket | ||
|
||
(define (sqrt x) | ||
(define (square n) (* n n)) | ||
(define (good-enough? previous-guess guess) | ||
(< (abs (/ (- guess previous-guess) guess)) 0.00000000001)) | ||
(define (average x y) | ||
(/ (+ x y) 2)) | ||
(define (improve guess) | ||
(average guess (/ x guess))) | ||
(define (sqrt-iter guess) | ||
(if (good-enough? guess (improve guess)) | ||
guess | ||
(sqrt-iter (improve guess)))) | ||
(sqrt-iter 1.0)) | ||
|
||
(provide sqrt) |