This file contains hidden or 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
public class Binary | |
{ | |
public static void main(String[] args) | |
{ | |
int N = Integer.parseInt(args[0]); | |
int v = 1; | |
while (v <= N/2) | |
v = v*2; | |
int n = N; | |
while (v > 0) |
This file contains hidden or 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
(define (smallest-divisor n) | |
(find-divisor n 2)) | |
(define (find-divisor n test-divisor) | |
(cond ((> (square test-divisor) n) n) | |
((divides? test-divisor n) test-divisor) | |
(else (find-divisor n (next test-divisor))))) | |
(define (divides? a b) | |
(= 0 (remainder b a))) |
This file contains hidden or 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
(define (expmod base exp m) | |
(cond ((= exp 0) 1) | |
((even? exp) | |
(remainder (square (expmod base (/ exp 2) m)) m)) | |
(else (remainder (* base (expmod base (- exp 1) m)) m)))) | |
(define (fermat-every n) | |
(fermat-helper (- n 1) n)) | |
(define (fermat-helper a n) |
This file contains hidden or 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
(define (integral f a b n) | |
(define h (/ (- b a) n)) | |
(define (i-next x) (+ x 1)) | |
(define (i-term x) | |
(cond ((or (= x 0) (= x n)) (f (+ a (* x h)))) | |
((even? x) (* 2 (f (+ a (* x h))))) | |
(else (* 4 (f (+ a (* x h))))))) | |
(* (/ h 3) (sum i-term 0 i-next n))) | |
This file contains hidden or 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
(define (pi n) | |
(define (p-term x) | |
(if (even? x) | |
(/ x (- x 1)) | |
(/ (- x 1) x))) | |
(define (p-next x) | |
(+ x 1)) | |
(* 4.0 (product p-term 3 p-next n))) |
This file contains hidden or 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
(define (filtered-accumulate combiner null-value term a next b predicate) | |
(if (> a b) | |
null-value | |
(combiner (if (predicate (term a)) | |
(term a) | |
null-value) | |
(filtered-accumulate combiner null-value term (next a) next b predicate)))) | |
This file contains hidden or 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
(define (cont-frac n d k) | |
(define (cont-help i result) | |
(if (= i k) | |
(/ 1.0 result) | |
(cont-help (+ i 1) (/ (n i) (+ (d i) result))))) | |
(cont-help 1 0.0)) | |
(cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 100) | |
;; A so-called k-term finite continued fraction | |
;; gives the value of the gold ratio |
This file contains hidden or 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
(define (cont-frac n d k) | |
(define (cont-help i result) | |
(if (= i 0) | |
result | |
(cont-help (- i 1) (/ (n i) (+ (d i) result))))) | |
(cont-help k 0.0)) | |
(cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 100) | |
;; A so-called k-term finite continued fraction | |
;; gives the value of the gold ratio |
This file contains hidden or 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
(define (d x) | |
(if (= (remainder x 3) 2) | |
(* 2 (+ (quotient x 3) 1)) | |
1)) | |
;; exercise 1.38, sicp |
This file contains hidden or 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
;; Exercise 1.39 | |
;; sicp | |
(define (tan-cf x k) | |
(define (n i) | |
(if (= i 1) | |
x | |
(square x))) | |
(define (d i) | |
(- (* 2 i) 1)) | |
(define (cont-frac n d k) |