Skip to content

Instantly share code, notes, and snippets.

@aymkx
Last active December 19, 2017 19:53
Show Gist options
  • Save aymkx/c56afcf213d7c951f809940b27f68da1 to your computer and use it in GitHub Desktop.
Save aymkx/c56afcf213d7c951f809940b27f68da1 to your computer and use it in GitHub Desktop.
そすうをつくります。
module PrimeNumber
def self.powmod(base, exp, mod)
return 1 if exp == 0
exp.bit_length.times.inject(1) do |buf, i|
(buf ** 2) * (base ** exp[exp.bit_length - 1 - i]) % mod
end
end
def self.miller_rabin_prime?(num, trial)
return false if num.even? || num < 3
odd_factor = num - 1
exp2 = 0
while odd_factor.even? do exp2 += 1; odd_factor >>= 1 end
srand()
trial.times do
witness = rand(2...num)
m = powmod(witness, odd_factor, num)
return true if m == 1
exp2.times do |i|
return true if m == num - 1
m = m ** 2 % num
end
end
return false
end
def self.generate_primenumber(len = 1024, miller_rabin_trial = 4)
rand_max = 1 << (len - 2)
q = 0
srand()
until miller_rabin_prime?(q, miller_rabin_trial)
q = ((rand(rand_max) + rand_max) << 1) + 1
end
return q
end
end
t = Time.now
prime = PrimeNumber.generate_primenumber.to_s(16)
t = Time.now - t
print prime, "\n\ngenerating time: ", t, "[s]\n\n"
system("echo #{prime}| clip")
system('pause')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment