Last active
November 11, 2020 23:52
-
-
Save hraban/ae88f879fc237a437cf56221b3fc4910 to your computer and use it in GitHub Desktop.
This file contains 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 n->r (n) | |
(let* ((band (isqrt n)) | |
(bandstart (expt band 2)) | |
(nextstart (expt (1+ band) 2))) | |
(list (min band (- n bandstart)) | |
(min band (- nextstart n 1))))) | |
(defun init-rar (size) | |
(let ((ar (make-array (list size size)))) | |
(dotimes (n (expt size 2)) | |
(destructuring-bind (x y) (n->r n) | |
(setf (aref ar y x) n))) | |
ar)) | |
(let ((ar (init-rar 5))) | |
(print ar)) | |
;; => #2A(( 0 3 8 15 24) | |
;; ( 1 2 7 14 23) | |
;; ( 4 5 6 13 22) | |
;; ( 9 10 11 12 21) | |
;; (16 17 18 19 20)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment