Skip to content

Instantly share code, notes, and snippets.

@philipp-spiess
Last active August 29, 2015 14:03
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 philipp-spiess/33a12b86970aec8fe731 to your computer and use it in GitHub Desktop.
Save philipp-spiess/33a12b86970aec8fe731 to your computer and use it in GitHub Desktop.
def zeuge(a, n)
b = (n-1).to_s(2) # binary representation
d = 1
b.reverse.split('').each do |i|
d = (d*d) % n
if i.to_i == 1
d = (d*a) % n
end
puts "#{i}: #{d}"
end
if d != 1
puts true
else
puts false
end
end
zeuge(ARGV[0].to_i, ARGV[1].to_i)
# Output
# ruby millerrobin.rb 11 161
# 7: 1
# 6: 1
# 5: 1
# 4: 1
# 3: 1
# 2: 11
# 1: 121
# 0: 51
# true => n is not a prim number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment