Tuesday, November 12, 2013

Problem 62 - Lisp

Lisp is a nice language, I personally prefer the Scheme dialect to Common Lisp, but I have no major qualms with it. This problem wasn't too bad...however, my solution right now is pretty slow (~2-3 min). The last time I had this issue (the other day of Prob. 60), I found a way to make the solution much faster the next day...so I will take the same approach here, and consider myself done, but I will look tomorrow to see if I can tweak it or find a better solution.
(defun digits (x)
  (if (< x 10) (cons x '()) 
      (cons (mod x 10) (digits (floor (/ x 10))))))

(defun cubes (size)
  (defun help (elem acc)
    (if (< (log (expt elem 3) 10) (- size 1)) acc 
        (help (- elem 1) (cons (expt elem 3) acc))))
  (help (floor (expt 10 (/ size 3))) '()))

(defun lsum (lst)
  (defun help (l tot)
    (if (equal l '()) tot (help (cdr l) (+ tot (car l)))))
  (help lst 0))

(defun ans (lst)
  (if (equal lst '()) 0
    (let ((digs (sort (digits (car lst)) #'>)))
    (let ((a (lsum (map 'list #'(lambda (x) 
        (if (equal digs (sort (digits x) #'>)) 1 0)) (cdr lst)))))
        (if (= a 4) (car lst) (ans (cdr lst)))))))

(defun e62 (size)
  (let ((a (ans (cubes size)))) (if (= a 0) (e62 (+ size 1)) a)))

(e62 5)

No comments:

Post a Comment