-
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.
- Loading branch information
Dzmitry Pasiukevich
committed
Oct 15, 2019
0 parents
commit 069338b
Showing
23 changed files
with
702 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,21 @@ | ||
; f(n) = n if n < 3 else f(n-1) + 2f(n-2) + 3f(n-3) | ||
|
||
(define (f-recur n) | ||
(if (< n 3) | ||
n | ||
(+ (f-recur (- n 1)) | ||
(* 2 (f-recur (- n 2))) | ||
(* 3 (f-recur (- n 3)))) | ||
) | ||
) | ||
|
||
(define (f-iter n) | ||
(define (iter count a b c) ; a = f(n-1), b = f(n-2), c = f(n-3) | ||
(if (= count 0) | ||
c | ||
(iter (- count 1) (+ a (* 2 b) (* 3 c)) a b) | ||
) | ||
) | ||
(iter n 2 1 0) | ||
) | ||
|
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,6 @@ | ||
(define (pascal-triangle r c) | ||
(if (or (= r 0) (= c 0) (= r c)) | ||
1 | ||
(+ (pascal-triangle (- r 1) c) (pascal-triangle (- r 1) (- c 1))) | ||
) | ||
) |
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,10 @@ | ||
(define (fast-expt b n) | ||
(define (square n) (* n n)) | ||
(define (iter a b n) | ||
(cond ((= n 0) a) | ||
((even? n) (iter a (square b) (/ n 2))) | ||
(else (iter (* a b) b (- n 1))) | ||
) | ||
) | ||
(iter 1 b n) | ||
) |
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,32 @@ | ||
; iterative, linear | ||
(define (slow-mult a b) | ||
(define (mult accum count) | ||
(if (= count 1) | ||
accum | ||
(mult (+ accum a) (- count 1)) | ||
) | ||
) | ||
(cond ((or (= a 0) (= b 0)) 0) | ||
((= a 1) b) | ||
(else (mult a b)) | ||
) | ||
) | ||
|
||
; iterative, logatithmic | ||
(define (fast-mult a b) | ||
(define (double n) (* n 2)) | ||
(define (half n) (/ n 2)) | ||
(define (iter a b) | ||
(cond ((= b 1) a) | ||
((even? b) (iter (double a) (half b))) | ||
(else (iter (+ a b) (- b 1))) | ||
) | ||
) | ||
(cond ((or (= a 0) (= b 0)) 0) | ||
((= a 1) b) | ||
(else (iter a b)) | ||
) | ||
) | ||
|
||
(fast-mult 45 8) | ||
(slow-mult 45 8) |
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,24 @@ | ||
(define (fib n) | ||
(define (square x) (* x x)) | ||
(define (fib-iter a b p q count) | ||
(cond ((= count 0) b) | ||
((even? count) | ||
(fib-iter a | ||
b | ||
(+ (square p) (square q)) | ||
(+ (square q) (* 2 p q)) | ||
(/ count 2) | ||
) | ||
) | ||
(else (fib-iter (+ (* b q) (* a q) (* a p)) | ||
(+ (* b p) (* a q)) | ||
p | ||
q | ||
(- count 1) | ||
)) | ||
) | ||
) | ||
(fib-iter 1 0 0 1 n) | ||
) | ||
|
||
(fib 20) |
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,22 @@ | ||
(load "prime-test.scm") | ||
|
||
(define (timed-prime-test func func-name num) | ||
(define (start-test start-time) | ||
(if (func num) (display-report (- (runtime) start-time))) | ||
) | ||
(define (display-report elapsed-time) | ||
(display func-name) (display " taken: ") (display elapsed-time) (newline) | ||
) | ||
(start-test (runtime)) | ||
) | ||
|
||
;(timed-prime-test prime? "prime?" 9880469) | ||
;(timed-prime-test prime-unoptimized? "prime-unoptimized?" 9880470) | ||
|
||
(define (fast-prime-test times) | ||
(define (wrapped num) | ||
(fast-prime? num times) | ||
) | ||
wrapped | ||
) | ||
;(timed-prime-test (fast-prime-test 5) "fast-prime?" 9880469) |
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,10 @@ | ||
(load "1.22_1.23_1.24_benchmarks.scm") | ||
|
||
(define (bench-test num) | ||
(timed-prime-test prime? "prime?" num) | ||
(timed-prime-test prime-unoptimized? "prime-unoptimized?" num) | ||
(timed-prime-test (fast-prime-test 5) "fast-prime?" num) | ||
) | ||
|
||
(bench-test 9880469) | ||
(bench-test 101030101) |
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,16 @@ | ||
(load "procedures_as_arguments.scm") | ||
|
||
(define (integrate-simpsons-rule f a b n) | ||
(define h (/ (- b a) n)) | ||
(define (multiplier index) | ||
(cond ((or (= index 0) (= index n)) 1.0) | ||
((even? index) 2.0) | ||
(else 4.0))) | ||
(define (term index) | ||
(* (multiplier index) | ||
(f (+ a (* index h))))) | ||
(* h (sum term 0 inc n))) | ||
|
||
|
||
(integrate-simpsons-rule cube 0 1 100) | ||
(integrate-simpsons-rule cube 0 1 1000) |
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 @@ | ||
(load "procedures_as_arguments.scm") | ||
|
||
(define (approximate-pi n) | ||
(define start 3) | ||
(define (term index) | ||
(define s (square index)) | ||
(/ (- s 1) s)) | ||
(define (next index) (+ index 2)) | ||
(* 4.0 (product term start next (+ (* 2 (+ n 1)) ; generated indices: 3, 5, 7, 9 ... | ||
start))) | ||
) | ||
|
||
(approximate-pi 10) | ||
(approximate-pi 100) | ||
(approximate-pi 1000) |
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,12 @@ | ||
(load "prime-test.scm") | ||
(load "procedures_as_arguments.scm") | ||
|
||
(define (sum-squares-of-primes a b) | ||
(define (prime? n) (fast-prime? n 15)) | ||
(filtered-accumulate prime? + 0 identity a inc b)) | ||
|
||
(define (func-with-nice-filter n) | ||
(define (filter-func i) (= (gcd i n) 1)) | ||
(filtered-accumulate filter-func * 1 identity 1 inc n)) | ||
|
||
(func-with-nice-filter 20) |
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,3 @@ | ||
(load "fixed_point.scm") | ||
|
||
(fixed-point (lambda (x) (+ 1 (/ 1.0 x))) 1.0) |
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 @@ | ||
(load "fixed_point.scm") | ||
|
||
; no average dumping | ||
(fixed-point (lambda (x) (/ (log 1000) (log x))) 2.0) | ||
(fixed-point (lambda (x) (+ (/ (log 1000) (* 2 (log x))) | ||
(/ x 2)) | ||
) 2.0) |
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,43 @@ | ||
(load "procedures_as_arguments.scm") | ||
|
||
(define (cont-frac N D k) | ||
(define (combiner negated-index result) | ||
(let ((index (* -1.0 negated-index))) | ||
(/ (N index) (+ (D index) result)))) | ||
(accumulate combiner 0 identity (* -1.0 k) inc -1.0)) | ||
|
||
(define (cont-frac-alt N D k) | ||
(define (loop result term) | ||
(if (= term 0) | ||
result | ||
(loop (/ (n term) | ||
(+ (d term) result)) | ||
(- term 1)))) | ||
(loop 0 k)) | ||
|
||
(/ 1.0 (cont-frac (lambda (i) 1.0) | ||
(lambda (i) 1.0) | ||
10)) ; approximation of golden ratio ~ 1.618 | ||
|
||
; 1.38 | ||
(+ 2 (cont-frac (lambda (i) 1.0) | ||
(lambda (i) (if (= (remainder i 3) 2) | ||
(/ (+ i 1) 1.5) | ||
1)) | ||
10)) ; approximation of e | ||
|
||
(define (tan-cf-alt x k) | ||
(define square-x (- (* x x))) | ||
(cont-frac-alt (lambda (i) (if (= i 1) x square-x)) | ||
(lambda (i) (- (* 2.0 i) 1)) | ||
k)) | ||
|
||
(define (tan-cf x k) | ||
(define square-x (- (* x x))) | ||
(cont-frac (lambda (i) (if (= i 1) x square-x)) | ||
(lambda (i) (- (* 2.0 i) 1)) | ||
k)) | ||
|
||
(tan-cf 90 200) | ||
(tan-cf-alt 90 200) | ||
;(tan-cf 30 10) |
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,24 @@ | ||
(define (average x y) | ||
(/ (+ x y) 2.0)) | ||
|
||
(define (improve guess x) | ||
(average guess (/ x guess))) | ||
|
||
(define (sqrt-iter guess x) | ||
; will recurse as both then-clause and else-clause are evaluated before checking predicate | ||
(new-if (good-enough? guess x) | ||
guess | ||
(sqrt-iter (improve guess x) x))) | ||
|
||
(define (square x) (* x x)) | ||
|
||
(define (good-enough? guess x) | ||
(< (abs (- (square guess) x)) 0.0001)) | ||
|
||
(define (new-if predicate then-clause else-clause) | ||
(cond (predicate then-clause) | ||
(else else-clause) | ||
) | ||
) | ||
|
||
(define (sqrt x) (sqrt-iter 1.0 x)) |
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,33 @@ | ||
(define (average x y) | ||
(/ (+ x y) 2.0) | ||
) | ||
|
||
(define (improve guess x) | ||
(average guess (/ x guess)) | ||
) | ||
|
||
(define (sqrt-iter prev-guess guess x) | ||
(if (small-diff? guess x) | ||
guess | ||
(sqrt-iter guess (improve guess x) x) | ||
) | ||
) | ||
|
||
(define (square x) (* x x)) | ||
|
||
(define (good-enough? guess x) | ||
(< (abs (- (square guess) x)) 0.0001) | ||
) | ||
|
||
(define (small-diff? x y) | ||
(<= (abs (- x y)) | ||
(* x 0.0001) | ||
) | ||
) | ||
|
||
(define (sqrt x) | ||
(sqrt-iter 2.0 1.0 x) | ||
) | ||
|
||
(sqrt 1023423842374029342342342) | ||
(sqrt 0.0000000001) |
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,30 @@ | ||
(define (average x y) | ||
(/ (+ x y) 2.0) | ||
) | ||
|
||
; improve guess for cube root | ||
(define (improve guess x) | ||
(/ (+ (/ x (square guess)) | ||
(* 2 guess)) | ||
3) | ||
) | ||
|
||
(define (sqrt-iter prev-guess guess x) | ||
(if (small-diff? prev-guess guess) | ||
guess | ||
(sqrt-iter guess (improve guess x) x) | ||
) | ||
) | ||
|
||
(define (square x) (* x x)) | ||
|
||
(define (small-diff? x y) | ||
(<= (abs (- x y)) | ||
(* x 0.0001) | ||
) | ||
) | ||
|
||
(define (sqrt x) | ||
(sqrt-iter 2.0 1.0 x) | ||
) | ||
|
Oops, something went wrong.