Skip to content

Instantly share code, notes, and snippets.

@ponkore
ponkore / f.rb
Created April 12, 2012 12:00
Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Ruby 3rd)
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
module ActsAsCsv
class CsvRow
def initialize(headers, arr)
@headers = headers
@arr = arr
end
@ponkore
ponkore / Problem1.clj
Created April 22, 2012 13:17
Project Euler Problem 1
;;; 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)))
;;; テスト
@ponkore
ponkore / Problem2.clj
Created April 22, 2012 13:26
Project Euler Problem 2
;;; 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])))
@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]
@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 / 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 / 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 / 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 / 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 / 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))