Skip to content

Instantly share code, notes, and snippets.

View antimon2's full-sized avatar

GOTOH Shunsuke antimon2

View GitHub Profile
@antimon2
antimon2 / gist:960956
Created May 7, 2011 23:26 — forked from maehrm/gist:960807
rw3sat.rb
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
def random_walk_3_sat(f, n, rr) # W1: R を rr に変更
ff = f.split(/\s*and\s*/)
rr.times do
x = Array.new(n) { [true, false].sample } # W4: a を x に変更
(3*n).times do
return "充足可能である" if ff.all? { |c| eval(c) } # W7,8,9
c = ff.find { |cc| !eval(cc) } # W10
@antimon2
antimon2 / rw3sat.js
Created May 11, 2011 12:57
rw3sat.js
function random_walk_3_sat(f, n, rr) { // W1
var ff = f.split(/\s*and\s*/).map(function (c) {
return {
toString: function () { return c; },
eval: Function("x", "return " + c.replace(/\bor\b/g, "||") + ";")
};
});
for (var r = 0; r < rr; r++) {
var x = random_walk_3_sat_w4(n); // W4
for (var k = 0; k < 3 * n; k++) {
@antimon2
antimon2 / rw3sat.coffee
Created May 31, 2011 17:22
rw3sat.coffee
random_walk_3_sat = (f, n, rr) ->
ff = (x) -> f.every (fn) -> fn x
for r in [1..rr]
x = sample [false, true] for i in [1..n]
for k in [1..3*n]
return "充足可能である" if ff x # W7,8,9
c = sample (fn for fn in f when not fn x) # W10
idx = parseInt sample c.toString().match(/\d+/g) # W11
x[idx] = not x[idx] # W12
"おそらく充足不可能である"
@antimon2
antimon2 / prime_each_from.rb
Created April 29, 2012 11:18
add `each_from` method (`each` with lower_bound) in class Prime.
require 'prime'
class Prime
def each_from lower_bound = 0, upper_bound = nil
return to_enum :each_from, lower_bound, upper_bound unless block_given?
if lower_bound.is_a? Range
range = lower_bound
lower_bound = range.first
upper_bound = range.exclude_end? ? range.last - 1 : range.last
end
flg = false
@antimon2
antimon2 / integer_isqrt.rb
Created May 2, 2012 03:22
Integer#isqrt - sqrt in Integer precision
class Integer
def isqrt
return 0.0/0 if self < 0
return self if self < 2
return 1 if self < 4
# === Newton's Method (in Integer precision) ===
n = self.div 2 # self / 2
while n * n > self
on = n
n = (n * n + self).div(2 * n)
@antimon2
antimon2 / collatz.rb
Created May 3, 2012 09:51
Integer#collatz
class Integer
def collatz &block
return to_enum :collatz unless block_given?
return self if self < 0
block[self]
n = self
until n < 2
n = n.even? ? n/2 : n*3+1 # <- *1
block[n]
end
@antimon2
antimon2 / collatz.py
Created May 5, 2012 10:08
collatz.py
def collatz(n):
if n >= 0:
yield n
while n > 1:
n = n / 2 if n % 2 == 0 else n * 3 + 1
yield n
# list(num for num in collatz(3))
# => [3, 10, 5, 16, 8, 4, 2, 1]
# len(list(num for num in collatz(27)))
# => 112
@antimon2
antimon2 / rw3sat.r
Created May 27, 2012 12:04
rw3sat.r
random_walk_3_sat <- function (f, n, rr) {
# f の各要素に x を適用した結果を Vector で返すクロージャ
ff <- (function (f) {
return(function (x) sapply(f, eval, list(x=x)))
})(f)
for (r in seq(rr)) {
# W4
x <- sample(c(FALSE,TRUE), n, replace=TRUE)
for (k in seq(3*n)) {
# W7,8,9
@antimon2
antimon2 / rw3sat.py
Created May 28, 2012 12:14
rw3sat.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import re
def random_walk_3_sat(f, n, rr):
ff = re.split(r'\s*and\s*', f)
for r in xrange(rr):
x = [random.choice([False, True]) for i in xrange(n)] # W4
for k in xrange(3 * n):
@antimon2
antimon2 / prime_sexy.rb
Created June 6, 2012 10:26
Prime#sexy_pair
require 'prime'
class Prime
def sexy_pair n = nil
return to_enum :sexy_pair, n unless block_given?
each(n).each_cons(3) do |p1, p2, p3|
yield [p1, p2] if p2 - p1 == 6
yield [p1, p3] if p3 - p1 == 6
end
end