1 12687dd9 2023-08-04 jrmu ;; The first three lines of this file were inserted by DrScheme. They record metadata
2 12687dd9 2023-08-04 jrmu ;; about the language level of this file in a form that our tools can easily process.
3 12687dd9 2023-08-04 jrmu #reader(lib "htdp-intermediate-reader.ss" "lang")((modname |23.3|) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp") (lib "arrow.ss" "teachpack" "htdp") (lib "gui.ss" "teachpack" "htdp")))))
4 12687dd9 2023-08-04 jrmu ;! : N -> number
5 12687dd9 2023-08-04 jrmu ;Computes the factorial of n.
7 12687dd9 2023-08-04 jrmu (define (! n)
9 12687dd9 2023-08-04 jrmu [(zero? n) 1]
10 12687dd9 2023-08-04 jrmu [else (* n (! (sub1 n)))]))
12 12687dd9 2023-08-04 jrmu ;series : (N[>=0] -> number) N[>=0] -> number
13 12687dd9 2023-08-04 jrmu ;Given a-term and n, determine the sum of the first n-th terms in the sequence a-term.
15 12687dd9 2023-08-04 jrmu (define (series a-term n)
17 12687dd9 2023-08-04 jrmu [(zero? n) (a-term n)]
18 12687dd9 2023-08-04 jrmu [else (+ (a-term n)
19 12687dd9 2023-08-04 jrmu (series a-term (sub1 n)))]))
21 12687dd9 2023-08-04 jrmu ;;e-taylor : N -> number
22 12687dd9 2023-08-04 jrmu ;;Given i, find the i-th term of the Taylor series e^x = 1 + x + (x^2)/2! ...
24 12687dd9 2023-08-04 jrmu ;e-power : number N -> number
25 12687dd9 2023-08-04 jrmu ;e-power computes the x-th power of e by summing up n terms of the Taylor sequence.
27 12687dd9 2023-08-04 jrmu (define (e-power x n)
28 12687dd9 2023-08-04 jrmu (local ((define (e-taylor i)
29 12687dd9 2023-08-04 jrmu (/ (expt x i) (! i))))
30 12687dd9 2023-08-04 jrmu (series e-taylor n)))
34 12687dd9 2023-08-04 jrmu (define (inexact-e-power x n)
35 12687dd9 2023-08-04 jrmu (exact->inexact (e-power x n)))
37 12687dd9 2023-08-04 jrmu (inexact-e-power 1 1000)
39 12687dd9 2023-08-04 jrmu (- (inexact-e-power 1 100) (exp 1))
42 12687dd9 2023-08-04 jrmu ;ln-taylor : N -> number
43 12687dd9 2023-08-04 jrmu ;Computes the n-th term of the taylor sequence for ln (x).
45 12687dd9 2023-08-04 jrmu ;ln-x : number N -> number
46 12687dd9 2023-08-04 jrmu ;Computes ln(x) by summing the first n-th terms of the Taylor sequence.
48 12687dd9 2023-08-04 jrmu (define (ln-x x n)
49 12687dd9 2023-08-04 jrmu (local ((define (ln-taylor i)
50 12687dd9 2023-08-04 jrmu (* 2 (/ (expt (/ (- x 1)
52 12687dd9 2023-08-04 jrmu (+ (* 2 i) 1))
53 12687dd9 2023-08-04 jrmu (+ (* 2 i) 1)))))
54 12687dd9 2023-08-04 jrmu (series ln-taylor n)))
56 12687dd9 2023-08-04 jrmu ;sin-x : number N -> number
57 12687dd9 2023-08-04 jrmu ;Determines sin(x) by summing the first n-th terms of the Taylor sequence.
59 12687dd9 2023-08-04 jrmu ;sin-taylor : N -> number
60 12687dd9 2023-08-04 jrmu ;Returns the i-th term of the Taylor sequence for sin(x).
62 12687dd9 2023-08-04 jrmu (define (sin-x x n)
63 12687dd9 2023-08-04 jrmu (local ((define (sin-taylor i)
64 12687dd9 2023-08-04 jrmu (* (/ (expt x (+ (* 2 i) 1))
65 12687dd9 2023-08-04 jrmu (! (+ (* 2 i) 1)))
67 12687dd9 2023-08-04 jrmu [(even? i) 1]
68 12687dd9 2023-08-04 jrmu [(odd? i) -1]))))
69 12687dd9 2023-08-04 jrmu (series sin-taylor n)))
71 12687dd9 2023-08-04 jrmu ;pi-greg : N -> number
72 12687dd9 2023-08-04 jrmu ;Calculates pi by summing the first n-th terms of the Taylor sequence.
74 12687dd9 2023-08-04 jrmu ;greg : N -> number
75 12687dd9 2023-08-04 jrmu ;Given i, find the n-th term of the greg series.
77 12687dd9 2023-08-04 jrmu (define (pi-greg n)
78 12687dd9 2023-08-04 jrmu (local ((define (greg i)
79 12687dd9 2023-08-04 jrmu (* 4 (* (/ 1
83 12687dd9 2023-08-04 jrmu [(even? i) 1]
84 12687dd9 2023-08-04 jrmu [(odd? i) -1])))))
85 12687dd9 2023-08-04 jrmu (series greg n)))