Last active
August 29, 2015 14:03
-
-
Save philipp-spiess/33a12b86970aec8fe731 to your computer and use it in GitHub Desktop.
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 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