在Ruby内建的mathn库中有方法可以直接判断整数是否为素数

 

 
  
  1. require 'mathn' 
  2.  
  3. puts 23849382941.prime? #=>true