Last active
August 29, 2015 13:55
-
-
Save tychobrailleur/8753725 to your computer and use it in GitHub Desktop.
Emacs Lisp experimentations
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 project-euler-1 (n) | |
"Implementation for Project Euler 1" | |
(let ((sum 0)) | |
(dotimes (i n) | |
(if (or (= 0 (% i 3)) (= 0 (% i 5))) | |
(setq sum (+ sum i)))) | |
sum)) | |
(project-euler-1 1000) | |
(defvar memoize (make-hash-table :test 'equal)) | |
(defun fibonacci (n) | |
"Implementation for Project Euler2" | |
(let (memoized) | |
(setq memoized (gethash (number-to-string n) memoize)) | |
(if memoized | |
(progn | |
memoized) | |
(cond ((= n 1) 1) | |
((= n 0) 1) | |
((setq return-value (+ (fibonacci (- n 1))(fibonacci (- n 2)))) | |
(puthash (number-to-string n) return-value memoize) | |
return-value))))) | |
(defun project-euler-2 (n) | |
(let ((sum 0) | |
(i 1) | |
(flag t)) | |
(while flag | |
(setq val (fibonacci i)) | |
(if (> val n) | |
(setq flag nil) | |
(progn | |
(if (= 0 (% val 2)) | |
(setq sum (+ sum val))))) | |
(setq i (+ i 1))) | |
sum)) | |
(fibonacci 10) | |
(project-euler-2 4000000) | |
(apply '+ (remove-if-not #'(lambda (x) (zerop (mod x 2))) '(1 2 3 4 5 6 7 8 9 10))) | |
(apply '+ (remove-if-not #'(lambda (n) (or (zerop (mod n 3)) (zerop (mod n 5)))) (number-sequence 1 999))) | |
(number-sequence 1 10) | |
(defun fizzbuzzp (n) | |
(or (zerop (mod n 3)) (zerop (mod n 5)))) | |
(loop for i from 1 to 999 when (fizzbuzzp i) summing i) | |
(remove-if #'(lambda (f) (or (string= "." f) (string= ".." f))) (directory-files ".")) | |
(loop with product = '() | |
for i from 1 to 10 | |
do (loop for j from 1 to 10 do (push (* i j) product)) | |
finally return (nreverse product)) | |
(print product) |
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
(require 'cl) | |
(defun square (n) | |
(* n n)) | |
(defun pythagorian (a b c) | |
(= (square c) (+ (square b) (square a)))) | |
(apply '* (loop named outer for a from 1 to 1001 | |
do (loop for b from a to 1001 do | |
(if (pythagorian a b (- (- 1000 a) b)) | |
(return-from outer (list a b (- (- 1000 a) b))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment