Skip to content

Instantly share code, notes, and snippets.

@ponkore
ponkore / Problem012.clj
Created May 5, 2012 09:03
Project Euler Problem 12
;;; The sequence of triangle numbers is generated by adding the natural numbers.
;;; So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
;;;
;;; 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
;;;
;;; Let us list the factors of the first seven triangle numbers:
;;;
;;; 1: 1
;;; 3: 1,3
;;; 6: 1,2,3,6
@ponkore
ponkore / Problem011.clj
Created April 30, 2012 01:42
Project Euler Problem 11
;;; In the 20x20 grid below, four numbers along a diagonal line have been marked in red.
;;;
;;; 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
;;; 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
;;; 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
;;; 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
;;; 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
;;; 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
;;; 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
;;; 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
@ponkore
ponkore / Problem010.clj
Created April 29, 2012 11:46
Project Euler Problem 10
;;; The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
;;; Find the sum of all the primes below two million.
(defn primes [n]
(loop [prime-list '(2) search-list (take-while #(< % n) (iterate inc 2))]
(let [prime-list-max (first prime-list)
search-list-next (filter #(not (zero? (mod % prime-list-max))) search-list)]
(if (< n (* prime-list-max prime-list-max))
(sort (concat prime-list search-list-next))
(recur (conj prime-list (first search-list-next)) search-list-next)))))
@ponkore
ponkore / Problem009.clj
Created April 26, 2012 12:29
Project Euler Problem 9
;;; A Pythagorean triplet is a set of three natural numbers, a b c, for which,
;;; a^2 + b^2 = c^2
;;; For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
;;; There exists exactly one Pythagorean triplet for which a + b + c = 1000.
;;; Find the product abc.
(defn problem9 [limit]
(let [lazy-seq (take limit (iterate inc 1))]
(for [a lazy-seq b lazy-seq c lazy-seq
:when (and (= (+ (* a a) (* b b)) (* c c))
@ponkore
ponkore / Problem008.clj
Created April 26, 2012 12:27
Project Euler Problem 8
;;; Find the greatest product of five consecutive digits in the 1000-digit number.
;;; problem8.txt に問題の数値を格納。
;;; 問題の数値文字列
(def problem8-str (apply str (re-seq #"\d+[^\n]" (slurp "src/sandbox/problem008.txt"))))
;;;=> "731671765313....63450"
(count problem8-str)
;;;=> 1000
(defn cut-nth [n] (Integer/parseInt (str (nth problem8-str n))))
(apply max (map #(* (cut-nth (+ 0 %))
@ponkore
ponkore / Problem7.clj
Created April 26, 2012 02:16
Project Euler Problem 7
;;; By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
;;; What is the 10 001st prime number?
(defn primes [n] (primes- '(2) (take (dec n) (iterate inc 2))))
(defn primes- [prime-list search-list]
(let [prime-list-max (first prime-list)
search-list-max (apply max search-list)
search-list-next (filter #(not (zero? (mod % prime-list-max))) search-list)]
(if (< search-list-max (* prime-list-max prime-list-max))
(sort (concat prime-list search-list-next))
@ponkore
ponkore / Problem6.clj
Created April 25, 2012 13:30
Project Euler Problem 6
;;; The sum of the squares of the first ten natural numbers is,
;;; 1^2 + 2^2 + ... + 10^2 = 385
;;; The square of the sum of the first ten natural numbers is,
;;; (1 + 2 + ... + 10)^2 = 55^2 = 3025
;;; Hence the difference between the sum of the squares of the first ten natural numbers
;;; and the square of the sum is 3025 - 385 = 2640.
;;; Find the difference between the sum of the squares of the first one hundred natural numbers
;;; and the square of the sum.
(defn calc-diffs [seq]
(let [square-sum (reduce + (map #(* % %) seq))
@ponkore
ponkore / Problem5.clj
Created April 25, 2012 13:17
Project Euler Problem 5
;;;2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
;;;What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
;;;2520 は 1 から 10 の数字の全ての整数で割り切れる数字であり、そのような数字の中では最小の値である。
;;;では、1 から 20 までの整数全てで割り切れる数字の中で最小の値はいくらになるか。
;;; 1から20までの数値全体での最小公倍数を求める。数値2つについては、
;;; (clojure.math.numeric-tower/lcm a b) で求められるので、reduce で畳み込む。
(use '[clojure.math.numeric-tower])
(reduce lcm (take 20 (iterate inc 1)))
@ponkore
ponkore / Problem4.clj
Created April 24, 2012 13:09
Project Euler Problem 4
;;;A palindromic number reads the same both ways.
;;;The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
;;;Find the largest palindrome made from the product of two 3-digit numbers.
;;;左右どちらから読んでも同じ値になる数を回文数という。 2桁の数の積で表される回文数のうち、
;;;最大のものは 9009 = 91 × 99 である。
;;;では、3桁の数の積で表される回文数のうち最大のものはいくらになるか。
;;; 回文判定
(defn palindromic? [str]
@ponkore
ponkore / Problem3.clj
Created April 24, 2012 11:58
Project Euler Problem 3
;;;The prime factors of 13195 are 5, 7, 13 and 29.
;;;What is the largest prime factor of the number 600851475143 ?
;;;13195 の素因数は 5、7、13、29 である。
;;;600851475143 の素因数のうち最大のものを求めよ。
;;; 素数のリストを求める。あまり巨大なリストにすると、計算が大変なので、
;;; 1000000 位にアタリをつける。
(defn primes [n] (primes- '(2) (take (dec n) (iterate inc 2))))
(defn primes- [prime-list search-list]