Archive

Archive for the ‘Programming’ Category

Common Lisp

April 3rd, 2011 Comments off

I’ll have to admit it.  I’m not much of an author.  I think everyone’s figured that out by now, though…  Anyway, I’ve been busy learning Common Lisp over the past month or so.  I’m still very shaky with it as it’s quite different than anything else I’ve programmed in.  My brother, who tries to use Lisp (via Clojure) any chance he gets, directed me towards Project Euler to help learn how to use the language.

I’ve now completed 5 problems.  None of them as efficient as I could have, but these are still the early days.  The last problem I completed was finding the sum of the digits of 100!.  For some reason, it gave me a great feeling when I completed this one.  It may be that I’m beginning to feel a bit better using Lisp or maybe that I’m not running in to as many road blocks as I was even yesterday, I dunno.  Anyway, to show it off, I’ll post the code to my ‘not-so-efficient’ answer to problem 20:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
(defun fac (n)
  (if (<= n 1)
      '1
    (apply #'* (loop for i from 1 to n collect i))))
 
(defun euler-problem-020 ()
  (let ((string-version "")
        (ch nil)
        (sum 0))
    (setf string-version (princ-to-string (fac 100)))
    (loop for i from 0 below (length string-version) do
          (setf ch (char string-version i))
          (setf sum (+ sum (parse-integer (princ-to-string ch)))))
    sum))
Categories: Programming Tags:

Bad Behavior has blocked 7 access attempts in the last 7 days.