Advertisement
timothy235

sicp-1-3-1-procedures-as-arguments

Feb 17th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 4.91 KB | None | 0 0
  1. #lang racket
  2.  
  3. ;;;;;;;;;;
  4. ;; 1.29 ;;
  5. ;;;;;;;;;;
  6.  
  7. (define (sum term a next b)
  8.   (if (> a b)
  9.     0
  10.     (+ (term a)
  11.        (sum term (next a) next b))))
  12.  
  13. (define (integral f a b dx)
  14.   (define (add-dx x) (+ x dx))
  15.   (* (sum f (+ a (/ dx 2.0)) add-dx b)
  16.      dx))
  17.  
  18. (define (cube x) (* x x x))
  19.  
  20. (integral cube 0 1 0.01)
  21. ;; 0.24998750000000042
  22. (integral cube 0 1 0.001)
  23. ;; 0.249999875000001
  24.  
  25. ;; Simpson's Rule:  Estimate the definite integral of f(x) from x = a to x = b as
  26. ;; [(b - a) / (* 3 n)] times
  27. ;; [(sum all terms) + (sum all inner terms) + 2 * (sum all odd inner terms)]
  28. ;; where n is an even number
  29.  
  30. (define (simpson-integral f a b even-n)
  31.   (define h (/ (- b a) even-n))
  32.   (define (add-dx x) (+ x h))
  33.   (define (add-two-dx x) (+ x h h))
  34.   (* (/ h 3.0)
  35.      (+ (sum f a add-dx b)
  36.         (sum f (+ a h) add-dx (- b h))
  37.         (* 2 (sum f (+ a h) add-two-dx (- b h))))))
  38.  
  39. (simpson-integral cube 0 1 100)
  40. ;; 0.25
  41. (simpson-integral cube 0 1 1000)
  42. ;; 0.25
  43.  
  44. ;;;;;;;;;;
  45. ;; 1.30 ;;
  46. ;;;;;;;;;;
  47.  
  48. ;; iterative-sum is tail-recursive hence iterative
  49.  
  50. (define (iterative-sum term a next b)
  51.   (define (iter a result)
  52.     (if (> a b)
  53.       result
  54.       (iter (next a) (+ result (term a)))))
  55.   (iter a 0))
  56.  
  57. (define (identity x) x)
  58.  
  59. (iterative-sum identity 1 add1 100) ; sum numbers from 1 to 100
  60. ;; 5050
  61.  
  62. ;;;;;;;;;;
  63. ;; 1.31 ;;
  64. ;;;;;;;;;;
  65.  
  66. (define (product term a next b)
  67.   (if (> a b)
  68.     1
  69.     (* (term a) (product term (next a) next b))))
  70.  
  71. (define (factorial n)
  72.   (product identity 1 add1 n))
  73.  
  74. (factorial 3)
  75. ;; 6
  76. (factorial 4)
  77. ;; 24
  78. (factorial 5)
  79. ;; 120
  80.  
  81. (define (iterative-product term a next b)
  82.   (define (iter a result)
  83.     (if (> a b)
  84.       result
  85.       (iter (next a) (* result (term a)))))
  86.   (iter a 1))
  87.  
  88. (iterative-product identity 1 add1 5) ; should be factorial(5)
  89. ;; 120
  90.  
  91. ;; Wallis' formula:
  92. ;; pi / 4 = (2 / 3) * (4 / 3) * (4 / 5) * (6 / 5) * (6 / 7) * (8 / 7) ...
  93.  
  94. (define (wallis-term i)
  95.   (define j (* 2 (add1 (quotient i 2))))
  96.   (if (odd? i)
  97.     (/ j (add1 j))
  98.     (/ j (sub1 j))))
  99.  
  100.  
  101. (for ([i (in-range 1 7)]) (displayln (wallis-term i)))
  102. ;; 2/3
  103. ;; 4/3
  104. ;; 4/5
  105. ;; 6/5
  106. ;; 6/7
  107. ;; 8/7
  108.  
  109. (* 4 (product wallis-term 1.0 add1 10))
  110. ;; 3.275101041334807
  111. (* 4 (product wallis-term 1.0 add1 100))
  112. ;; 3.1570301764551645
  113. (* 4 (product wallis-term 1.0 add1 1000))
  114. ;; 3.143160705532257
  115. (* 4 (product wallis-term 1.0 add1 10000))
  116. ;; 3.1417497057379635
  117. (* 4 (product wallis-term 1.0 add1 100000))
  118. ;; 3.1416083612780903
  119.  
  120. ;;;;;;;;;;
  121. ;; 1.32 ;;
  122. ;;;;;;;;;;
  123.  
  124. (define (accumulate combiner null-value term a next b)
  125.   (if (> a b)
  126.     null-value
  127.     (combiner (term a)
  128.               (accumulate combiner null-value term (next a) next b))))
  129.  
  130. (define (new-sum term a next b)
  131.   (accumulate + 0 term a next b))
  132.  
  133. (new-sum identity 1 add1 100) ; add numbers from 1 to 100
  134. ;; 5050
  135.  
  136. (define (new-product term a next b)
  137.   (accumulate * 1 term a next b))
  138.  
  139. (new-product identity 1 add1 5) ; should be factorial(5)
  140. ;; 120
  141.  
  142. (define (iterative-accumulate combiner null-value term a next b)
  143.   (define (iter a result)
  144.     (if (> a b)
  145.       result
  146.       (iter (next a) (combiner result (term a)))))
  147.   (iter a null-value))
  148.  
  149. (iterative-accumulate + 0 identity 0 add1 100) ; add numbers from 1 to 100
  150. ;; 5050
  151.  
  152. (iterative-accumulate * 1 identity 1 add1 5) ; should be factorial(5)
  153. ;; 120
  154.  
  155. ;;;;;;;;;;
  156. ;; 1.33 ;;
  157. ;;;;;;;;;;
  158.  
  159. (define (filtered-accumulate filter? combiner null-value term a next b)
  160.   (define (iter a result)
  161.     (cond [(> a b) result]
  162.           [(filter? a)
  163.            (iter (next a) (combiner result (term a)))]
  164.           [else (iter (next a) result)]))
  165.   (iter a null-value))
  166.  
  167. (define (prime? n [times 10]) ; miller-rabin test from 1.26
  168.   (define (expmod base expo m)
  169.     (cond [(zero? expo) 1]
  170.           [(even? expo)
  171.            (define u (expmod base (/ expo 2) m))
  172.            (define u-squared (remainder (sqr u) m))
  173.            (if (and (= u-squared 1)
  174.                     (> u 1)
  175.                     (< u (sub1 m)))
  176.              0
  177.              u-squared)]
  178.           [else (remainder (* base (expmod base (sub1 expo) m)) m)]))
  179.   (define (try-it a)
  180.     (= (expmod a (sub1 n) n) 1))
  181.   (if (< n 10)
  182.     (member n '(2 3 5 7))
  183.     (for/and ([i (in-range times)])
  184.              ; do not test 0, 1, or n - 1
  185.              (try-it (+ 2 (random (min 4294967087 (- n 3))))))))
  186.  
  187. (define (sum-of-squares-of-primes a b)
  188.   (filtered-accumulate prime? + 0 sqr a add1 b))
  189.  
  190. (sum-of-squares-of-primes 1 4) ; 2 ^ 2 + 3 ^ 2 = 13
  191. ;; 13
  192. (sum-of-squares-of-primes 1 10) ; 4 + 9 + 25 + 49 = 87
  193. ;; 87
  194.  
  195. (define (product-of-relative-primes n)
  196.   (define (relatively-prime? i)
  197.     (= (gcd i n) 1))
  198.   (filtered-accumulate relatively-prime? * 1 identity 1 add1 n))
  199.  
  200. (product-of-relative-primes 6) ; 1 * 5 = 5
  201. ;; 5
  202. (product-of-relative-primes 7) ; 1 * 2 * 3 * 4 * 5 * 6 = 720
  203. ;; 720
  204. (product-of-relative-primes 8) ; 1 * 3 * 5 * 7 = 105
  205. ;; 105
  206. (product-of-relative-primes 20)
  207. ;; 8729721
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement