The lisp-way to solve Fibonnaci

前端 未结 14 1682
别那么骄傲
别那么骄傲 2021-02-08 07:17

I wanted to try and learn Lisp, but I very quickly gave up. I figured I\'d try again. I\'m looking at Problem 2 on Project Euler - finding the sum of all even Fibonacci numbers

14条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-08 07:57

    ;;; I try to write code as suggestion of @PESTO

    (defun Fibo-Test (n / one_prior two_prior curr sum i)

    (setq i 2) (setq one_prior 1 two_prior 1 )

    (cond

    ((= n 0) (setq sum 0) )

    ((= n 1) (setq sum 1) )

    ((= n 2) (setq sum 1) )

    (T

    (while (and (< i n) (< sum (* 4 1e6)))

    ;;; convert to Real-num

    (setq sum (+ (float one_prior) (float two_prior)))

    (setq i (1+ i))

    (setq curr sum) (setq
    one_prior two_prior two_prior curr
    )

    ) ; end while

    ) ; end cond(T)

    ) ; end cond

    (princ (strcat "\nF(" (itoa i) ") = " (RTOS sum) " . "))

    (princ)

    ) ; end function Fibo-Test

提交回复
热议问题