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
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
module ActsAsCsv | |
class CsvRow | |
def initialize(headers, arr) | |
@headers = headers | |
@arr = arr | |
end |
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
;;; Project Euler Problem 1 | |
;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%201 | |
;;; | |
;;; 10未満の自然数のうち、3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり、 これらの合計は 23 になる。 | |
;;; 同じようにして、1,000 未満の 3 か 5 の倍数になっている数字の合計を求めよ。 | |
;;; 3 もしくは 5の倍数の場合 true、そうでない場合 false を返す filter 判定用関数 | |
(defn is-3-or-5-mul? [n] (or (= (mod n 3) 0) (= (mod n 5) 0))) | |
;;; テスト |
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
;;; Project Euler Problem 2 | |
;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%202 | |
;;; | |
;;; フィボナッチ数列の項は前の2つの項の和である。 最初の2項を 1, 2 とすれば、最初の10項は以下の通りである。 | |
;;; 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... | |
;;; 数列の項の値が400万を超えない範囲で、偶数値の項の総和を求めよ。 | |
;;; フィボナッチ数列を、シーケンスとして求める。 | |
(defn fib1 [[a b]] [b (+' a b)]) | |
(defn fib [] (map second (iterate fib1 [1 1]))) |
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
;;;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] |
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
;;;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] |
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
;;;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))) |
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
;;; 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)) |
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
;;; 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)) |
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
;;; 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 %)) |
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
;;; 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)) |