Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / grep.clj
Created April 12, 2012 04:09
Cheap grep by Clojure
;;;
;;; 簡単 grep。のつもりが、あんまりかっこ良くないorz
;;;
(use '[clojure.java.io :as io])
(defn indexed [coll]
(map vector (iterate inc 1) coll))
(defn grep [re file]
(with-open [rdr (io/reader file)]
@ponkore
ponkore / b.rb
Created April 11, 2012 12:23
Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Ruby 2nd)
#!/usr/bin/env ruby
arr = (1..16).to_a
tmp = []
arr.each do |val|
tmp.push(val)
if tmp.length == 4
puts tmp.join(",")
tmp = []
end
end
@ponkore
ponkore / 1..10-sequence-result.txt
Created April 11, 2012 10:14
Seven Languages in Seven Weeks: A Pragmatic Guide to Learning Programming Languages (Ruby 1st)
irb(main):008:0> (1..10).each { |i| puts "This is sentence number #{i}" }
This is sentence number 1
This is sentence number 2
This is sentence number 3
This is sentence number 4
This is sentence number 5
This is sentence number 6
This is sentence number 7
This is sentence number 8
This is sentence number 9
@ponkore
ponkore / gcd-1.clj
Created April 3, 2012 01:04
ユークリッド互除法による最大公約数
;; mod を普通に使う
(defn gcd [m n]
(if (zero? n) m
(gcd n (mod m n))))
(gcd 1763 1927)
; => 41
@ponkore
ponkore / groovyserver.txt
Created March 28, 2012 03:04
groovy & groovyserv installation on OSX Lion
# groovy client with no arguments (startup server)
bash$ groovyclient
Invoking server: 'groovyserver' -p 1961
Groovy home directory: (none)
Groovy command path: /opt/local/bin/groovy (found at PATH)
GroovyServ home directory: /opt/local/share/devel/groovyserv
GroovyServ work directory: /Users/masao/.groovy/groovyserv
Original classpath: (none)
GroovyServ default classpath: /opt/local/share/devel/groovyserv/lib/*
Starting.....
@ponkore
ponkore / fibo.clj
Created March 26, 2012 14:21
fibo.clj
;; 何のひねりもない fibo
(defn fibo [x]
(if (or (= 0 x) (= 1 x))
1
(+ (fibo (- x 1)) (fibo (- x 2)))))
;;
(map #(fibo %) (take 10 (iterate inc 1)))
;; => (1 2 3 5 8 13 21 34 55 89)
@ponkore
ponkore / fizzbuzz.clj
Created March 26, 2012 04:23
FizzBuzz in Clojure
;; FizzBuzz in Clojure
(defn fizzbuzz [max]
(map (fn [n]
(cond
(= (mod n 15) 0) "FizzBuzz"
(= (mod n 3) 0) "Fizz"
(= (mod n 5) 0) "Buzz"
:else n))
(take max (iterate inc 1))))