3.1
Write a program that computes the arithmetic sum of a constant difference sequence:
D0 = A Dn+1 = Cdn + B
Run the following values and compare to the closed form solution:
Maximum index = 10,000, a = 1:0; c = 5; b = 2^-15
I am not sure I perfectly understand the problem above or how to confirm that I have the correct answer, but I wrote the following short program and I would appreciate any feedback.
(defun d-n-plus-1 (d-n c b) (+ (* d-n c) b))
(defun d-depth (depth d-n c b a)
(if (equal depth 0) (d-n-plus-1 d-n c b)
(d-depth (1- depth)
(if (null d-n) a (d-n-plus-1 d-n c b))
c b a)))
(format t "result: ~a ~%" (d-depth 10000 nil 5 (expt 2 -15) 1))
In addition, I wrote a version that just uses a loop. I'm not crazy about "loop for x from 1 to depth" or that I used setq in this version. Can you think of a better way? Any other suggestions for this version are also welcome.
(defun d_n+1 (d_n c b) (+ (* d_n c) b))
(defun d-depth (depth d_n c b)
(loop for x from 0 to depth do
(setq d_n (d_n+1 d_n c b)))
d_n)
(format t "result: ~a ~%" (d-depth 10000 1 5 (expt 2 -15)))