Skip to content

Instantly share code, notes, and snippets.

@vznvzn
Created April 27, 2016 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vznvzn/6371aa7392293e13815b8479e337dc3a to your computer and use it in GitHub Desktop.
Save vznvzn/6371aa7392293e13815b8479e337dc3a to your computer and use it in GitHub Desktop.
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