费马素性检验

2015-12-08 0 783
费马素性检验
def fermat(num, iter)
  #num is the number to test, iter is the maximum number of iterations (higher is more accurate)
  rand = 1+rand(num-1)
  for i in 1..iter
    if (rand**(num-1))%num == 0
      return false
    else
      rand = 1+rand(num-1)
    end
  end
  return true
end

#Example
puts fermat(16, 100) #=>false
puts fermat(7, 100)  #=>true
puts fermat(221, 100) #=>Counterexample. Called a Fermat Liar. Returns true (if the iter is low enough (usually)) actually is composite

遇见资源网 ruby 费马素性检验 http://www.ox520.com/16554.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务