Last active
June 10, 2019 19:30
-
-
Save kupp1/97dfa1a4921bf8ba4b6f2bd0a5458cd1 to your computer and use it in GitHub Desktop.
Syracuse sequence on Common Lisp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defvar *n* (read)) | |
(defvar *counter* 0) | |
(do () ((= *n* 1) *n*) | |
(setf *counter* (1+ *counter*)) | |
(format t "~d->~d~%" | |
*n* | |
(if (= (mod *n* 2) 0) | |
(let ((n1 (/ *n* 2))) (setf *n* n1) n1) | |
(let ((n1 (1+ (* *n* 3)))) (setf *n* n1) n1)))) | |
(format t "Loops in ~d steps~%" *counter*) | |
(exit) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun syracuse(n) | |
(if (= n 1) | |
'(1) | |
(if (= (mod n 2) 0) | |
(cons n (syracuse (/ n 2))) | |
(cons n (syracuse (1+ (* n 3))))))) | |
(format t "~A~%" (syracuse (read))) | |
(exit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment