Project Euler

Problem 22

(require 'calc-ext) (defvar calc-command-flags nil) (with-temp-buffer (insert-file-contents-literally (car argv)) (let (name-lst (i 1) (sum 0)) (while (re-search-forward "\"\\([A-Z]+?\\)\"" nil t) (setq name-lst (cons (match-string-no-prop…

Problem 21

(require 'cl) (require 'calc-ext) (defvar calc-command-flags nil) (defun hash-to-alist (hash) (let (alist) (maphash #'(lambda (key val) (push (cons key val) alist)) hash) alist )) (defun find-factor (num) (catch 'found (let ((i 2)) (while …

Problem 20

(require 'calc-ext) (defvar calc-command-flags nil) (defun factorial (n) (let ((rst 1)) (while (math-lessp 0 n) (setq rst (math-mul rst n)) (setq n (math-sub n 1)) ) rst )) (defun digit-sum (n) (let* ((str (math-format-value n)) (len (leng…

Problem 19

(defvar month-day-alist '((1 . 31) (2 . 28) (3 . 31) (4 . 30) (5 . 31) (6 . 30) (7 . 31) (8 . 31) (9 . 30) (10 . 31) (11 . 30) (12 . 31))) (defun assq-val (key alist) (cdr (assq key alist))) (defun next-month (year month day) (let ((days (…

Probrem 18

(defun read-txt-numbers (file-name) (let (num-lst) (with-temp-buffer (insert-file-contents-literally file-name) (goto-char (point-min)) (while (re-search-forward "[0-9]+" nil t) (setq num-lst (cons (string-to-number (match-string 0)) num-l…

Problem 17

(defvar num-str-alist '((1 . "one") (2 . "two") (3 . "three") (4 . "four") (5 . "five") (6 . "six") (7 . "seven") (8 . "eight") (9 . "nine") (10 . "ten") (11 . "eleven") (12 . "twelve") (13 . "thirteen") (14 . "fourteen") (15 . "fifteen") …

Problem 15

(require 'calc-ext) (defvar calc-command-flags nil) (defun factorial (n) (let ((rst 1)) (while (math-lessp 0 n) (setq rst (math-mul rst n)) (setq n (math-sub n 1)) ) rst )) (let ((size (string-to-number (car argv)))) (princ (math-format-va…

Problem 14

(require 'calc-ext) (defvar calc-command-flags nil) (let ((limit (math-add (math-read-number (car argv)) 1)) (hash (make-hash-table)) (i 1) (max-i 1)) (while (math-lessp i limit) (let ((n i) (len 0) found) (while (not (math-equal n 1)) (if…

Problem 16

(require 'calc-ext) (defvar calc-command-flags nil) (defun convert-decimal-to-binary (d) (let (b (i 0)) (while (> d 0) (setq b (cons (if (eq (% d 2) 0) 0 1) b)) (setq d (/ d 2)) (setq i (1+ i)) ) b)) (defun digits-sum (str) (let ((sum 0)) …

Problem 13

(require 'calc-ext) (defvar calc-command-flags nil) (with-temp-buffer (insert-file-contents-literally (car argv)) (let ((sum 0)) (while (re-search-forward "[0-9]\\{50\\}" nil t) (setq sum (math-add sum (math-read-number (match-string-no-pr…

Problem 11

(require 'cl) (defun make-ad-lst (pivot len grid-size) (let* ((idx (1- len)) (right (loop for i from 0 to idx collect i)) (down (loop for i from 0 to idx collect (* grid-size i))) (right-down (loop for i from 0 to idx collect (+ (* grid-si…

Problem 10

(require 'calc-ext) (defvar calc-command-flags nil) (defun find-factor (num) (catch 'found (let ((i 2)) (while (math-lessp (math-sub i 1) (math-floor (math-sqrt num))) (if (math-equal (math-mod num i) 0) (throw 'found i) (setq i (math-add …

Problem 12

(require 'cl) (require 'calc-ext) (defvar calc-command-flags nil) (defun find-factor (num) (catch 'found (let ((i 2)) (while (math-lessp (math-sub i 1) (math-floor (math-sqrt num))) (if (math-equal (math-mod num i) 0) (throw 'found i) (set…

Problem 9

(let* ((n (string-to-number (car argv))) (a 1) b c ans) (setq ans (catch 'found (while (<= a (/ n 3)) (setq b (1+ a)) (while (<= b (/ (- n a) 2)) (setq c (- n a b)) (if (= (+ (* a a) (* b b)) (* c c)) (throw 'found (list a b c)) ) (setq b …

Problem 8

(require 'cl) (let* ((str "\ 73167176531330624919225119674426574742355349194934\ 96983520312774506326239578318016984801869478851843\ 85861560789112949495459501737958331952853208805511\ 12540698747158523863050715693290963295227443043557\ 66…

Problem 7

(require 'calc-ext) (defvar calc-command-flags nil) (defun find-factor (num) (catch 'found (let ((i 2)) (while (math-lessp (math-sub i 1) (math-floor (math-sqrt num))) (if (math-equal (math-mod num i) 0) (throw 'found i) (setq i (math-add …

Problem 6

(require 'cl) (defun make-1-to-n-list (n) (loop for i from 1 to n collect i)) (defun 2pow (x) (* x x)) (let* ((num-lst (make-1-to-n-list (string-to-number(car argv)))) (x (apply #'+ (mapcar #'2pow num-lst))) (y (2pow (apply #'+ num-lst))))…

Problem 5

(require 'cl) (defun make-1-to-n-list (n) (loop for i from 1 to n collect i)) (princ (apply #'lcm (make-1-to-n-list (string-to-number (car argv))))) (princ "\n") 追記 make-1-to-n-list を定義しなくても, emacs の標準関数により汎用的な関数 nu…

Problem 4

(defun mirror-num (num) (let* ((str (int-to-string num)) (len (length str)) elt) (dotimes (i (floor (/ len 2))) (setq elt (aref str i)) (aset str i (aref str (1- (- len i)))) (aset str (1- (- len i)) elt)) (string-to-int str) )) (let ((i 1…

Problem 3

(require 'calc-ext) (defvar calc-command-flags nil) (defun find-factor (num) (catch 'found (let ((i 2)) (while (math-lessp (math-sub i 1) (math-floor (math-sqrt num))) (if (math-equal (math-mod num i) 0) (throw 'found i) (setq i (math-add …

Problem 2

(let ((a 1) (b 1) (c 0) (rst 0)) (while (< a 4000000) (if (eq (% a 2) 0) (setq rst (+ rst a)) ) (setq c a) (setq a (+ a b)) (setq b c) ) (princ rst) (princ "\n"))

Problem 1

(let ((rst 0)) (dotimes (i 1000) (if (or (eq (% i 3) 0) (eq (% i 5) 0) ) (setq rst (+ rst i)) )) (princ rst) (princ "\n") ) elisp でやっていくぜよ