Skip to content

Commit

Permalink
Add the isPrimeNumber method (#4)
Browse files Browse the repository at this point in the history
* add required extension: ext-gmp
* update the PHP version
* remove checking PHP 5.6
* add allow_failure for hhvm and 5.6
* remove hhvm and php 5.6
* add the isPrimeNumber method.
* modify the test prime number method
  • Loading branch information
peter279k authored and waltertamboer committed Nov 10, 2017
1 parent 7bf5273 commit d6dc1b3
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ public function subtract(string $value): BigInteger
return $this->assignValue($calculatedValue);
}

/**
* Checks if the big integr is the prime number.
*
* @param float $probabilityFactor A normalized factor between 0 and 1 used for checking the probability.
* @return bool Returns true if the number is a prime number false if not.
*/
public function isPrimeNumber(float $probabilityFactor = 1.0): bool
{
$reps = (int)floor(($probabilityFactor * 5.0) + 5.0);

if ($reps < 5 || $reps > 10) {
throw new InvalidArgumentException('The provided probability number should be 5 to 10.');
}

return gmp_prob_prime($this->value, $reps) !== 0;
}

/**
* Checks if this object is mutable.
*
Expand Down
75 changes: 75 additions & 0 deletions tests/BigIntegerPrimeNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace PHP\Math\BigIntegerTest;

use PHP\Math\BigInteger\BigInteger;
use PHPUnit_Framework_TestCase;

class BigIntegerPrimeNumberTest extends PHPUnit_Framework_TestCase
{
public function testIsPrimeNumberWithouttPrimeNumber()
{
// Arrange
$bigInteger = new BigInteger('6');

// Act
$withoutPrimeNumber = $bigInteger->isPrimeNumber();

// Assert
$this::assertFalse($withoutPrimeNumber);
}

public function testIsPrimeNumberWithPrimeNumber()
{
// Arrange
$bigInteger = new BigInteger('11');

// Act
$withPrimeNumber = $bigInteger->isPrimeNumber();

// Assert
$this::assertTrue($withPrimeNumber);
}

public function testIsPrimeNumberWithoutProbabilePrimeNumber()
{
// Arrange
$bigInteger = new BigInteger('1111111111111111111');

// Act
$probabilePrimeNumber = $bigInteger->isPrimeNumber();

// Assert
$this::assertTrue($probabilePrimeNumber);
}

/**
* @expectedException InvalidArgumentException
*/
public function testIsPrimeNumberProvidedProbabilityNumberIsLessThan5()
{
// Arrange
$bigInteger = new BigInteger('1111111111111111111');

// Act
$probabilePrimeNumber = $bigInteger->isPrimeNumber(-1.0);

// Assert
// ...
}

/**
* @expectedException InvalidArgumentException
*/
public function testIsPrimeNumberProvidedProbabilityNumberIsGreaterThan10()
{
// Arrange
$bigInteger = new BigInteger('1111111111111111111');

// Act
$probabilePrimeNumber = $bigInteger->isPrimeNumber(2.0);

// Assert
// ...
}
}

0 comments on commit d6dc1b3

Please sign in to comment.