[前][次][番号順一覧][スレッド一覧]

ruby-changes:40119

From: marcandre <ko1@a...>
Date: Tue, 20 Oct 2015 12:18:28 +0900 (JST)
Subject: [ruby-changes:40119] marcandRe: r52200 (trunk): * lib/prime.rb: Optimize Integer#prime?

marcandre	2015-10-20 12:18:14 +0900 (Tue, 20 Oct 2015)

  New Revision: 52200

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=52200

  Log:
    * lib/prime.rb: Optimize Integer#prime?
      Patch by Nick Slocum [Bug #10354]

  Modified files:
    trunk/ChangeLog
    trunk/lib/prime.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 52199)
+++ ChangeLog	(revision 52200)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Oct 20 12:17:50 2015  Marc-Andre Lafortune  <ruby-core@m...>
+
+	* lib/prime.rb: Optimize Integer#prime?
+	  Patch by Nick Slocum [Bug #10354]
+
 Tue Oct 20 08:12:47 2015  Rei Odaira  <Rei.Odaira@g...>
 
 	* configure.in: pthread_getattr_np is broken on AIX.
Index: lib/prime.rb
===================================================================
--- lib/prime.rb	(revision 52199)
+++ lib/prime.rb	(revision 52200)
@@ -31,7 +31,14 @@ class Integer https://github.com/ruby/ruby/blob/trunk/lib/prime.rb#L31
 
   # 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.

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]