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 -*- | |
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 |
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
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++) { |
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
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 | |
"おそらく充足不可能である" |
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
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 |
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
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) |
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
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 |
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
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 |
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
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 |
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 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): |
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
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 |
OlderNewer