- def prime?(num)
- res = [1]
- res << num
- if num == 0 || num == 1
- return false
- end
- 2.upto(10) do |x|
- #如果有自己的话,就跳下一次循环
- if num == x
- next
- end
- #看看是否能被 2-10之间的数整除, 取余数相当于分组
- if num % x == 0
- res << x
- end
- end
- res.length > 2 ? false : true
- end
- #该片段来自于http://www.codesnippet.cn/detail/040520149460.html
来源: http://www.codesnippet.cn/detail/040520149460.html