Triangular Recursion Function
Posted: Thu Dec 08, 2011 9:02 pm
Hi,
I am trying to solve this question :
Exercise: The N'th triangular number is defined to be 1 + 2 + 3 + ... + N. Alternatively, we could give a recursive definition of triangular number as follows:
T(n) = 1 if n = 1
T(n) = n + T(n-1) if n > 1
Use the recursive definition to help you implement a linearly recursive function (triangular N) that returns the N'th triangular number. Enter your function definition into a text file. Then load it into LISP. Evaluate (triangular 6).
Below is my code :
(defun triangle (N)
"Compute the Triangular Number of N."
(if (= N 1)
1
(+ N (triangular (- N 1)))))
When I loaded it into the Debug Window using (load "CallTriangle") , it says Loading C:\acl82express\CallTriangle.cl. How do I evaluate (triangular 6) ?
Thanks
I am trying to solve this question :
Exercise: The N'th triangular number is defined to be 1 + 2 + 3 + ... + N. Alternatively, we could give a recursive definition of triangular number as follows:
T(n) = 1 if n = 1
T(n) = n + T(n-1) if n > 1
Use the recursive definition to help you implement a linearly recursive function (triangular N) that returns the N'th triangular number. Enter your function definition into a text file. Then load it into LISP. Evaluate (triangular 6).
Below is my code :
(defun triangle (N)
"Compute the Triangular Number of N."
(if (= N 1)
1
(+ N (triangular (- N 1)))))
When I loaded it into the Debug Window using (load "CallTriangle") , it says Loading C:\acl82express\CallTriangle.cl. How do I evaluate (triangular 6) ?
Thanks