1
0
Fork 0
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:
marcandre 2015-10-20 03:18:14 +00:00
parent 49efa6680c
commit 8744d5164d
2 changed files with 13 additions and 1 deletions

View file

@ -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> Tue Oct 20 08:12:47 2015 Rei Odaira <Rei.Odaira@gmail.com>
* configure.in: pthread_getattr_np is broken on AIX. * configure.in: pthread_getattr_np is broken on AIX.

View file

@ -31,7 +31,14 @@ class Integer
# Returns true if +self+ is a prime number, else returns false. # Returns true if +self+ is a prime number, else returns false.
def prime? 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 end
# Iterates the given block over all prime numbers. # Iterates the given block over all prime numbers.