I wrote a procedure golden-ratio
to compute the Golden Ratio. The first argument is the number where we start computing the golden ratio and the second argument is the number of times we'll be repeating the recursion.
(defun golden-ratio (start-num times)
(if (eq times 0)
start-num
(golden-ratio (+ (/ 1.0 start-num) 1) (- times 1))))
(golden-ratio 30 250)
gives the result 1.618033988749895. But when I try
(golden-ratio 30 300)
I get
Lisp nesting exceeds `max-lisp-eval-depth'
My question is, is there another algorithm that would let me go deeper than ~250?
Perhaps a better approach to the problem would be to concentrate on the desired accuracy rather than the number of repetitions. This way you could iteratively improve your partial result until the improvement rate got below the accuracy. I find the do
macro particularly suitable for this type of computation. You could approach the problem as follows:
(defun golden-ratio (start delta)
"Calculate the golden ratio starting at START with accuracy
DELTA."
(do ((old (float start))
(new (1+ (/ (float start))) (1+ (/ new))))
((< (abs (- old new)) delta) new)
(setq old new)))
This way you can do:
ELISP> (golden-ratio 1 1.0e-40)
1.618033988749895