Skip to content

Instantly share code, notes, and snippets.

@esehara
esehara / multi.ml
Last active April 22, 2016 07:53
掛け算の実装
open Pervasives
(*
例えば、掛け算は、「足し算をn回繰りかえす」と定義できる
*)
let rec mul1 x n =
if n == 1
then x
else x + (mul1 x (n - 1))
@esehara
esehara / file0.txt
Created April 17, 2016 10:10
MiniTestで一つのファイルにテストを混ぜてしまおう ref: http://qiita.com/esehara@github/items/40a74251b0a67b5b6520
# もっといい方法があるが、サンプルのために、このままにする
def exchange_node x, coin
return 1 if x == 0
return 1 if x < 5
return 0 if x < 0
sum_coin = 0
case coin
when 10
sum_coin += exchange_node(x - 5, 5)
@esehara
esehara / money.rb
Last active April 17, 2016 11:29
SICP P.22 両替の問題について
def exchange x
exchange_node x, 50
end
# proto exchange node example
def proto_exchange_node x, coin
return 1 if x == 0
return 1 if x < 5
return 0 if x < 0
@esehara
esehara / wheel.rb
Last active April 15, 2016 04:30
wheel factorizationとか言うやつ
# M_kを、k番目までの素数の積を合計したものとする。
# 例えばM2ならば2 * 3で6となる。これがホイールの
# 周期として定義される。
# M2の場合、最初の素数以降、確率的に2, 4, 2, 4...
# の間隔で、素数の候補が表われる。これらは、自分の理解
# するところによれば、2と3の倍数が表れない周期性を利用
# している。
M2_CYCLE_LENGTH = 6
@esehara
esehara / atkin.rb
Last active May 19, 2018 16:12
アトキンのふるい
class SieveHelper
def initialize(limit, x, y)
@limit = limit
@x = x
@y = y
end
def invalid?
(@x ** 2) + (@y ** 2) >= @limit
end
@esehara
esehara / prime-fact.rkt
Created April 12, 2016 00:16
素因数分解するやつ
#lang racket
(require rackunit)
;; factor x y
;;
;; 割りきれるとき ->引き数と割った後の数
;; 割りきれないとき ->空リストを返す
(define (factor x y)
(if (= 0 (modulo x y))
@esehara
esehara / README.md
Last active April 11, 2016 07:08
某ソシャゲSNSのためのお役立ちスクリプト

省略

@esehara
esehara / file0.ml
Created September 19, 2015 16:46
OCaml完全マスターした (OCamlでFizzBuzzを書いてみよう) ref: http://qiita.com/esehara@github/items/bfb05d84ee1f8357276a
let hello = print_string ("Hello, World." ^ "\n")
let () = hello
@esehara
esehara / file0.ml
Last active September 14, 2015 16:51
CodeIQとかで"module Str"を使ったsplitが使えないので、自前で何とかsplitするやつを書く ref: http://qiita.com/esehara@github/items/894d8dd824b1354752f5
Str.split (Str.regexp_string ",") line
@esehara
esehara / 0825.clj
Created August 28, 2015 12:56
出社準備完了 2015/08/25
;; sample
;; http://quil.info/sketches/show/example_hyper
(ns shussha-kanryo.hyper
(:require [quil.core :as q :include-macros true]
[quil.middleware :as m]))
(defn setup []
(q/frame-rate 60)
(q/background 255)