Skip to content

Commit

Permalink
Merge pull request #11 from drupol/issue-10-fix-issue-with-factorial
Browse files Browse the repository at this point in the history
Issue #10: Do not use a formula to compute the count.
  • Loading branch information
drupol committed May 29, 2018
2 parents ff8a6e9 + b7305ab commit 4dc6c2c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/Iterators/Combinations.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ public function valid()
*/
public function count()
{
return $this->fact(count($this->getDataset())) /
($this->fact($this->getLength()) * $this->fact(count($this->getDataset()) - $this->getLength()));
$i = 0;

for ($this->rewind(); $this->valid(); $this->next()) {
++$i;
}

return $i;
}

/**
Expand Down
14 changes: 13 additions & 1 deletion tests/src/Iterators/CombinationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function testCombinations($input, $expected)

$this->assertEquals($input['dataset'], $combinations->getDataset());
$this->assertEquals($input['length'], $combinations->getLength());
$this->assertEquals(count($input['dataset']), count($combinations->getDataset()));
$this->assertCount(count($input['dataset']),
$combinations->getDataset());
$this->assertEquals(
$expected['dataset'],
$combinations->toArray(),
Expand All @@ -40,4 +41,15 @@ public function testCombinations($input, $expected)
);
$this->assertEquals($expected['count'], $combinations->count());
}

/**
* Test combinations with big numbers.
*
* @see https://github.com/drupol/phpermutations/issues/10
*/
public function testCombinationsWithBigNumbers()
{
$combinations = new Combinations(range(1, 200), 2);
$this->assertCount($combinations->count(), $combinations->toArray());
}
}

0 comments on commit 4dc6c2c

Please sign in to comment.