Skip to content

Commit 4dc6c2c

Browse files
authored
Merge pull request #11 from drupol/issue-10-fix-issue-with-factorial
Issue #10: Do not use a formula to compute the count.
2 parents ff8a6e9 + b7305ab commit 4dc6c2c

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/Iterators/Combinations.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,13 @@ public function valid()
8686
*/
8787
public function count()
8888
{
89-
return $this->fact(count($this->getDataset())) /
90-
($this->fact($this->getLength()) * $this->fact(count($this->getDataset()) - $this->getLength()));
89+
$i = 0;
90+
91+
for ($this->rewind(); $this->valid(); $this->next()) {
92+
++$i;
93+
}
94+
95+
return $i;
9196
}
9297

9398
/**

tests/src/Iterators/CombinationsTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public function testCombinations($input, $expected)
2929

3030
$this->assertEquals($input['dataset'], $combinations->getDataset());
3131
$this->assertEquals($input['length'], $combinations->getLength());
32-
$this->assertEquals(count($input['dataset']), count($combinations->getDataset()));
32+
$this->assertCount(count($input['dataset']),
33+
$combinations->getDataset());
3334
$this->assertEquals(
3435
$expected['dataset'],
3536
$combinations->toArray(),
@@ -40,4 +41,15 @@ public function testCombinations($input, $expected)
4041
);
4142
$this->assertEquals($expected['count'], $combinations->count());
4243
}
44+
45+
/**
46+
* Test combinations with big numbers.
47+
*
48+
* @see https://github.com/drupol/phpermutations/issues/10
49+
*/
50+
public function testCombinationsWithBigNumbers()
51+
{
52+
$combinations = new Combinations(range(1, 200), 2);
53+
$this->assertCount($combinations->count(), $combinations->toArray());
54+
}
4355
}

0 commit comments

Comments
 (0)