Created
April 27, 2016 01:21
-
-
Save vznvzn/6371aa7392293e13815b8479e337dc3a to your computer and use it in GitHub Desktop.
This file contains 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 fsmrun(n, fsm) | |
l = n.to_s(2).split('') | |
p(l) | |
l2 = [] | |
s = 0 | |
l.each \ | |
{ | |
|b| | |
l2 << [b.to_i, s = fsm[[s, b.to_i]]] | |
} | |
p(l2) | |
return s | |
end | |
def fsmdiv(r) | |
fsm = {} | |
r.times \ | |
{ | |
|x| | |
x0 = (x * 2) % r | |
x1 = (x * 2 + 1) % r | |
fsm[[x, 0]] = x0 | |
fsm[[x, 1]] = x1 | |
} | |
p(fsm) | |
n = rand(1000) | |
s = fsmrun(n, fsm) | |
puts(["n = #{n}", "r = #{r}", "s = #{s}", "n % r = #{n % r}", "s == n % r: #{s == n % r}"].join("\t")) | |
puts | |
end | |
l = ARGV.map { |x| x.to_i } | |
p(l) | |
puts | |
l.each { |r| fsmdiv(r) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment