Created
September 29, 2014 18:48
-
-
Save bigos/87aca37517f59860074d to your computer and use it in GitHub Desktop.
Question: How to optimise Lisp
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 expotential-divisors (n expot) | |
(reverse (loop | |
for y from 1 upto n | |
for x = 1 then (* x expot ) | |
collect x))) | |
(defun expot-len (n) | |
(1- (expt 2 n))) | |
(defun calc-base (n divs a b) | |
(loop for d in divs | |
sum (if (zerop (floor (/ n d))) | |
a | |
b) | |
do (if (>= n d) (setf n (rem n d))))) | |
(defun puzzle (n a b) | |
(let* ((divs (expotential-divisors n 2)) | |
(res) (found)) | |
(loop for x from 0 to (expot-len n) | |
do (progn | |
(setf res (calc-base x divs a b)) | |
(unless (search (list res) found) | |
(push res found) | |
(format t "~a " res )))))) | |
(defun find-values (n a b) | |
(puzzle (1- n) a b) | |
(terpri)) | |
;;; running | |
(find-values 3 2 1) | |
;;; should give: | |
4 3 2 | |
;;; there are problems with values of n above 20 | |
;;; (find-values 25 2 1) | |
;;; Lisp begins to choke |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In https://gist.github.com/dimitri/d5120b069e601edc312a you will find a solution that does not choke at all on your problem size: