mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/prime.rb (Prime::prime?): used to return a wrong answer.
[ruby-core:22646]. * test/test_prime.rb (test_prime?): test case for [ruby-core:22646]. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22741 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
27cc677088
commit
294f45b0fc
3 changed files with 24 additions and 0 deletions
|
@ -1,3 +1,10 @@
|
|||
Wed Mar 4 00:54:43 2009 Yuki Sonoda (Yugui) <yugui@yugui.jp>
|
||||
|
||||
* lib/prime.rb (Prime::prime?): used to return a wrong answer.
|
||||
[ruby-core:22646].
|
||||
|
||||
* test/test_prime.rb (test_prime?): test case for [ruby-core:22646].
|
||||
|
||||
Tue Mar 3 17:10:09 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||
|
||||
* ext/extmk.rb (extmake): removes object files no longer used and
|
||||
|
|
|
@ -144,6 +144,8 @@ class Prime
|
|||
# +value+:: an arbitrary integer to be checked.
|
||||
# +generator+:: optional. A pseudo-prime generator.
|
||||
def prime?(value, generator = Prime::Generator23.new)
|
||||
value = -value if value < 0
|
||||
return false if value < 2
|
||||
for num in generator
|
||||
q,r = value.divmod num
|
||||
return true if q < num
|
||||
|
|
|
@ -89,6 +89,11 @@ class TestPrime < Test::Unit::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
def test_default_instance_does_not_have_compatibility_methods
|
||||
assert !Prime.instance.respond_to?(:succ)
|
||||
assert !Prime.instance.respond_to?(:next)
|
||||
end
|
||||
|
||||
class TestInteger < Test::Unit::TestCase
|
||||
def test_prime_division
|
||||
pd = PRIMES.inject(&:*).prime_division
|
||||
|
@ -100,6 +105,10 @@ class TestPrime < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_prime?
|
||||
# zero and unit
|
||||
assert !0.prime?
|
||||
assert !1.prime?
|
||||
|
||||
# small primes
|
||||
assert 2.prime?
|
||||
assert 3.prime?
|
||||
|
@ -121,6 +130,12 @@ class TestPrime < Test::Unit::TestCase
|
|||
|
||||
# factorial
|
||||
assert !(2...100).inject(&:*).prime?
|
||||
|
||||
# negative
|
||||
assert !-1.prime?
|
||||
assert -2.prime?
|
||||
assert -3.prime?
|
||||
assert !-4.prime?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue