From 37be9418ad8f5bbde5f7b703115a2d4fb5d6185e Mon Sep 17 00:00:00 2001 From: Mistivia Date: Sat, 9 Nov 2024 01:26:37 +0800 Subject: 2 Even Fibonacci Numbers --- 0001/main.lisp | 6 +++--- 0002/main.lisp | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) mode change 100644 => 100755 0001/main.lisp create mode 100644 0002/main.lisp diff --git a/0001/main.lisp b/0001/main.lisp old mode 100644 new mode 100755 index c7ba165..0ce3d1d --- a/0001/main.lisp +++ b/0001/main.lisp @@ -1,13 +1,13 @@ (defun genlist (n) (defun impl (x lst) (if (> x n) - (reverse lst) - (impl (+ 1 x) (cons x lst)))) + (reverse lst) + (impl (+ 1 x) (cons x lst)))) (impl 1 '())) (let ((sum 0)) (loop for x in (genlist 999) - do (if (or (equal 0 (mod x 3)) + do (if (or (equal 0 (mod x 3)) (equal 0 (mod x 5))) (setf sum (+ sum x)))) (print sum)) diff --git a/0002/main.lisp b/0002/main.lisp new file mode 100644 index 0000000..df3e1cd --- /dev/null +++ b/0002/main.lisp @@ -0,0 +1,19 @@ +(defun new-next-fibo-generator () + (let ((x0 0) + (x1 1)) + (lambda () + (let ((next (+ x0 x1))) + (setf x0 x1) + (setf x1 next) + next)))) + +(defvar *uplimit* 4000000) + +(let ((next-fibo (new-next-fibo-generator)) + (sum 0)) + (do ((x (funcall next-fibo) (funcall next-fibo))) + ((> x *uplimit*) sum) + (setf sum (if (evenp x) + (+ sum x) + sum))) + (format t "~&~S" sum)) -- cgit v1.0