mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/prime.rb: Optimize Integer#prime?
Patch by Nick Slocum [Bug #10354] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52200 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
49efa6680c
commit
8744d5164d
2 changed files with 13 additions and 1 deletions
|
@ -1,3 +1,8 @@
|
|||
Tue Oct 20 12:17:50 2015 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
|
||||
|
||||
* lib/prime.rb: Optimize Integer#prime?
|
||||
Patch by Nick Slocum [Bug #10354]
|
||||
|
||||
Tue Oct 20 08:12:47 2015 Rei Odaira <Rei.Odaira@gmail.com>
|
||||
|
||||
* configure.in: pthread_getattr_np is broken on AIX.
|
||||
|
|
|
@ -31,7 +31,14 @@ class Integer
|
|||
|
||||
# Returns true if +self+ is a prime number, else returns false.
|
||||
def prime?
|
||||
Prime.prime?(self)
|
||||
return self >= 2 if self <= 3
|
||||
return false if self % 2 == 0 or self % 3 == 0
|
||||
(5..(self**0.5).floor).step(6).each do |i|
|
||||
if self % i == 0 || self % (i + 2) == 0
|
||||
return false
|
||||
end
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
# Iterates the given block over all prime numbers.
|
||||
|
|
Loading…
Add table
Reference in a new issue