Skip to content

Commit

Permalink
* lib/prime.rb (Prime#prime_division): now decomposes
Browse files Browse the repository at this point in the history
  negative integer into a decomposition with element [-1, 1].

* test/test_prime.rb: test for it.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24091 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
yugui committed Jul 13, 2009
1 parent 0fefd19 commit 7287b85
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Mon Jul 13 22:49:50 2009 Yuki Sonoda (Yugui) <yugui@yugui.jp>

* lib/prime.rb (Prime#prime_division): now decomposes
negative integer into a decomposition with element [-1, 1].

* test/test_prime.rb: test for it.

Mon Jul 13 22:28:03 2009 NAKAMURA Usaku <usa@ruby-lang.org>

* io.c (pipe_open): handles leaked on win32 when an error occurs.
Expand Down
7 changes: 6 additions & 1 deletion lib/prime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ def int_from_prime_division(pd)
#
def prime_division(value, generator= Prime::Generator23.new)
raise ZeroDivisionError if value == 0
pv = []
if value < 0
value = -value
pv = [[-1, 1]]
else
pv = []
end
for prime in generator
count = 0
while (value1, mod = value.divmod(prime)
Expand Down
5 changes: 5 additions & 0 deletions test/test_prime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,15 @@ class TestInteger < Test::Unit::TestCase
def test_prime_division
pd = PRIMES.inject(&:*).prime_division
assert_equal PRIMES.map{|p| [p, 1]}, pd

pd = (-PRIMES.inject(&:*)).prime_division
assert_equal [-1, *PRIMES].map{|p| [p, 1]}, pd
end

def test_from_prime_division
assert_equal PRIMES.inject(&:*), Integer.from_prime_division(PRIMES.map{|p| [p,1]})

assert_equal -PRIMES.inject(&:*), Integer.from_prime_division([[-1, 1]] + PRIMES.map{|p| [p,1]})
end

def test_prime?
Expand Down

0 comments on commit 7287b85

Please sign in to comment.