# Liutos/Project-Euler

first commit

0 parents commit 052fc1f1abb91d02ab76ef892cc938aa42076072 committed Feb 4, 2012
Showing with 80 additions and 0 deletions.
2. +7 −0 pro1.lisp
3. +19 −0 pro14.lisp
4. +9 −0 pro2.lisp
5. +28 −0 pro3.lisp
6. +13 −0 pro5.lisp
7. +3 −0 pro6.lisp
 @@ -0,0 +1 @@ +This repository collects my solutions of the problems in Project Euler written in Common Lisp. I can not solve every problems, yet. The file proXX.lisp is corresponding to the problem XX in Porject Euler.
7 pro1.lisp
 @@ -0,0 +1,7 @@ +(defun pro1 (num) + (let ((sum 0)) + (loop for i from 1 to (1- num) + do (if (or (zerop (mod i 3)) + (zerop (mod i 5))) + (incf sum i))) + sum))
19 pro14.lisp
 @@ -0,0 +1,19 @@ +(defun collatz-length (n) + (labels ((rec (m times) + (if (= 1 m) + times + (if (evenp m) + (rec (/ m 2) (1+ times)) + (rec (1+ (* 3 m)) (1+ times)))))) + (rec n 1))) + +(defun pro14 (n) + (let ((max nil) + (win 0)) + (loop + :for i from 2 upto n + :do (let ((len (collatz-length i))) + (if (> len win) + (setf max i + win len)))) + (values max win)))
9 pro2.lisp
 @@ -0,0 +1,9 @@ +(defun even-fib-sum (limit) + (labels ((rec (st nd sum) + (if (> nd limit) + sum + (rec nd (+ st nd) + (if (evenp nd) + (+ sum nd) + sum))))) + (rec 1 2 0)))
28 pro3.lisp
 @@ -0,0 +1,28 @@ +(defun primep (n) + (labels ((rec (test-num) + (if (> (* test-num test-num) n) + t + (if (zerop (mod n test-num)) + nil + (rec (1+ test-num)))))) + (rec 2))) + +(defun largest-prime-factor (n) + (labels ((rec (num acc) + (cond ((= 1 num) acc) + ((<= num 2) num) + (t (let ((fac (do ((i 2 (1+ i))) + ((or (>= i num) + (zerop (mod num i))) i)))) + (rec (/ num fac) fac)))))) + (rec n 1))) + +(defun largest-prime-factor (n) + (declare (number n)) + (let ((max 2)) + (declare (fixnum max)) + (loop for i from 3 to (- n 2) by 2 + do (if (zerop (mod n i)) + (if (primep i) + (setq max i))) + finally (return max))))
13 pro5.lisp
 @@ -0,0 +1,13 @@ +(defun helper (n lst) + (reduce #'(lambda (n d) + (if (zerop (rem n d)) + (/ n d) + n)) + lst :initial-value n)) + +(defun pro5 (n) + (labels ((rec (m lst) + (if (>= m n) + (apply #'* lst) + (rec (1+ m) (push (helper m lst) lst))))) + (rec 2 '(1))))
3 pro6.lisp
 @@ -0,0 +1,3 @@ +(defun difference (n) + (- (expt (/ (* n (1+ n)) 2) 2) + (loop for i from 1 upto n summing (expt i 2))))