문제

I am looking for some help with a LISP function.

I have to write a LISP function that defines a recursive version of the function:

Sq(1) = 1

Sq(n) = sq(n-1)+2*n – 1 if n>1

If anyone can point me to a helpful website or able to provide an answer, that would be greatly appreciated!

Thanks.

도움이 되었습니까?

해결책

Just do a 1-to-1 translation. Assuming you want Common Lisp, here's the implementation:

(defun sq (n)
  (if (= n 1)
      1
      (+ (sq (1- n)) n n -1)))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top