Last active
September 25, 2015 08:02
-
-
Save tamamu/8f9af701748ff9eb9e33 to your computer and use it in GitHub Desktop.
My answers of projecteuler.net problems.
This file contains hidden or 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
#| | |
Multiples of 3 and 5 | |
(https://projecteular.net/problem=1) | |
1,000以下の自然数で3か5の倍数である数の和を求める | |
|# | |
(defun sum-of-multipes-of-3-or-5 (m) | |
(loop for n | |
below m | |
when (or (eq (mod n 3) 0) | |
(eq (mod n 5) 0)) | |
sum n)) | |
(defun sum-of-multiple (x m) | |
(let ((n (floor (float (/ (1- m) x))))) | |
(* (/ (* n (1+ n)) 2) x))) | |
(defun solve-1 () | |
(sum-of-multipes-of-3-or-5 1000)) | |
(defun solve-2 () | |
(- (+ (sum-of-multiple 3 1000) (sum-of-multiple 5 1000)) (sum-of-multiple 15 1000))) |
This file contains hidden or 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
#| | |
Even Fibonacci numbers | |
https://projecteuler.net/problem=2 | |
4,000,000未満のフィボナッチ数列の偶数項の和を求める | |
|# | |
(defun fib (lst) | |
(cons (+ (car lst) (cadr lst)) lst)) | |
(defun fib-to-n (lst n) | |
(if (> (+ (car lst) (cadr lst)) n) | |
lst | |
(fib-to-n (fib lst) n))) | |
(defun sum-of-even (lst) | |
(loop for i | |
in lst | |
when (evenp i) | |
sum i)) | |
(defun solve-1 () | |
(sum-of-even (fib-to-n (list 3 2 1) 4000000))) |
This file contains hidden or 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
#| | |
Largest prime factor | |
https://projecteuler.net/problem=3 | |
600,851,475,143の最大素因数を求める | |
|# | |
(defun prime-list (num) | |
(labels ((f (n lst y) | |
(if (eq n 1) lst | |
(if (eq (mod n y) 0) (cons y (f (/ n y) lst y)) | |
(f n lst (1+ y)))))) | |
(f num () 2))) | |
(defun solve-1 () | |
(car (last (prime-list 600851475143)))) |
This file contains hidden or 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
#| | |
Largest palindrome product | |
https://projecteuler.net/problem=4 | |
3桁×3桁からなる回文数のうち最大の数を求める | |
|# | |
(defun is-palindrome (num) | |
(let ((lst (concatenate 'list (write-to-string num)))) | |
(labels ((f (n) | |
(if (>= n (list-length lst)) n | |
(if (eq (nth n lst) (nth (- (1- (list-length lst)) n) lst)) (f (1+ n)) | |
nil | |
)))) | |
(f 0)))) | |
(defun make-palindrome-list (i j) | |
(if (>= 100 i) nil | |
(if (>= 100 j) (make-palindrome-list (1- i) 999) | |
(if (is-palindrome (* i j)) (cons (* i j) (make-palindrome-list i (1- j))) | |
(make-palindrome-list i (1- j)))))) | |
(defun solve-1 () | |
(apply #'max (make-palindrome-list 999 999))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment