Пример #1
1
 /**
  * Finds next strong pseudoprime number, following after $num
  *
  * @param gmp resource $num
  * @return gmp resource
  * @access public
  */
 function nextPrime($num)
 {
     if (!gmp_cmp(gmp_mod($num, 2), 0)) {
         $num = gmp_sub($num, 1);
     }
     do {
         $num = gmp_add($num, 2);
     } while (!gmp_prob_prime($num));
     return $num;
 }
Пример #2
0
 function get_prime_number()
 {
     $primos = 0;
     $numeros = 0;
     do {
         $aux = gmp_prob_prime($numeros);
         if ($aux == 2) {
             $primos++;
         }
         $numeros++;
     } while ($primos <= 10001);
     return $numeros;
 }
Пример #3
0
 protected function calcularPrimo()
 {
     $x = 0;
     $i = 0;
     while ($i < 10001) {
         if ($this->isImpar($x) || $x == 2) {
             if (gmp_prob_prime($x) == 2) {
                 $primo = $x;
                 $i++;
             }
         }
         $x++;
     }
     return $primo;
 }
Пример #4
0
 /**
  * Checks a numer to see if it's prime
  *
  * Assuming the $t parameter is not set, this function has an error rate of 2**-80.  The main motivation for the
  * $t parameter is distributability.  Math_BigInteger::randomPrime() can be distributed across multiple pageloads
  * on a website instead of just one.
  *
  * @param Math_BigInteger $t
  * @return bool
  * @access public
  * @internal Uses the
  *     {@link http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Miller-Rabin primality test}.  See
  *     {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=8 HAC 4.24}.
  */
 function isPrime($t = false)
 {
     $length = strlen($this->toBytes());
     if (!$t) {
         // see HAC 4.49 "Note (controlling the error probability)"
         // @codingStandardsIgnoreStart
         if ($length >= 163) {
             $t = 2;
         } else {
             if ($length >= 106) {
                 $t = 3;
             } else {
                 if ($length >= 81) {
                     $t = 4;
                 } else {
                     if ($length >= 68) {
                         $t = 5;
                     } else {
                         if ($length >= 56) {
                             $t = 6;
                         } else {
                             if ($length >= 50) {
                                 $t = 7;
                             } else {
                                 if ($length >= 43) {
                                     $t = 8;
                                 } else {
                                     if ($length >= 37) {
                                         $t = 9;
                                     } else {
                                         if ($length >= 31) {
                                             $t = 12;
                                         } else {
                                             if ($length >= 25) {
                                                 $t = 15;
                                             } else {
                                                 if ($length >= 18) {
                                                     $t = 18;
                                                 } else {
                                                     $t = 27;
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         // @codingStandardsIgnoreEnd
     }
     // ie. gmp_testbit($this, 0)
     // ie. isEven() or !isOdd()
     switch (MATH_BIGINTEGER_MODE) {
         case MATH_BIGINTEGER_MODE_GMP:
             return gmp_prob_prime($this->value, $t) != 0;
         case MATH_BIGINTEGER_MODE_BCMATH:
             if ($this->value === '2') {
                 return true;
             }
             if ($this->value[strlen($this->value) - 1] % 2 == 0) {
                 return false;
             }
             break;
         default:
             if ($this->value == array(2)) {
                 return true;
             }
             if (~$this->value[0] & 1) {
                 return false;
             }
     }
     static $primes, $zero, $one, $two;
     if (!isset($primes)) {
         $primes = array(3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997);
         if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) {
             for ($i = 0; $i < count($primes); ++$i) {
                 $primes[$i] = new Math_BigInteger($primes[$i]);
             }
         }
         $zero = new Math_BigInteger();
         $one = new Math_BigInteger(1);
         $two = new Math_BigInteger(2);
     }
     if ($this->equals($one)) {
         return false;
     }
     // see HAC 4.4.1 "Random search for probable primes"
     if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) {
         foreach ($primes as $prime) {
             list(, $r) = $this->divide($prime);
             if ($r->equals($zero)) {
                 return $this->equals($prime);
             }
         }
     } else {
         $value = $this->value;
         foreach ($primes as $prime) {
             list(, $r) = $this->_divide_digit($value, $prime);
             if (!$r) {
                 return count($value) == 1 && $value[0] == $prime;
             }
         }
     }
     $n = $this->copy();
     $n_1 = $n->subtract($one);
     $n_2 = $n->subtract($two);
     $r = $n_1->copy();
     $r_value = $r->value;
     // ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s));
     if (MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH) {
         $s = 0;
         // if $n was 1, $r would be 0 and this would be an infinite loop, hence our $this->equals($one) check earlier
         while ($r->value[strlen($r->value) - 1] % 2 == 0) {
             $r->value = bcdiv($r->value, '2', 0);
             ++$s;
         }
     } else {
         for ($i = 0, $r_length = count($r_value); $i < $r_length; ++$i) {
             $temp = ~$r_value[$i] & 0xffffff;
             for ($j = 1; $temp >> $j & 1; ++$j) {
             }
             if ($j != 25) {
                 break;
             }
         }
         $s = 26 * $i + $j - 1;
         $r->_rshift($s);
     }
     for ($i = 0; $i < $t; ++$i) {
         $a = $this->random($two, $n_2);
         $y = $a->modPow($r, $n);
         if (!$y->equals($one) && !$y->equals($n_1)) {
             for ($j = 1; $j < $s && !$y->equals($n_1); ++$j) {
                 $y = $y->modPow($two, $n);
                 if ($y->equals($one)) {
                     return false;
                 }
             }
             if (!$y->equals($n_1)) {
                 return false;
             }
         }
     }
     return true;
 }
Пример #5
0
 public function testVerificarNumero2EPrimo()
 {
     $this->assertEquals(2, gmp_prob_prime(2));
 }
Пример #6
0
$pow1 = gmp_pow("2", 31);
echo gmp_strval($pow1) . "\n";
$pow2 = gmp_pow("0", 0);
echo gmp_strval($pow2) . "\n";
$pow3 = gmp_pow("2", -1);
// Negative exp, generates warning
echo gmp_strval($pow3) . "\n";
// gmp_powm
$pow1 = gmp_powm("2", "31", "2147483649");
echo gmp_strval($pow1) . "\n";
// gmp_prob_prime
echo gmp_prob_prime("6") . "\n";
// definitely not a prime
echo gmp_prob_prime("1111111111111111111") . "\n";
// probably a prime
echo gmp_prob_prime("11") . "\n";
// definitely a prime
// gmp_random -- not implemented
// gmp_scan0
// "0" bit is found at position 3. index starts at 0
$s1 = gmp_init("10111", 2);
echo gmp_scan0($s1, 0) . "\n";
// "0" bit is found at position 7. index starts at 5
$s2 = gmp_init("101110000", 2);
echo gmp_scan0($s2, 5) . "\n";
// gmp_scan1
// "0" bit is found at position 3. index starts at 0
$s1 = gmp_init("01000", 2);
echo gmp_scan1($s1, 0) . "\n";
// "0" bit is found at position 7. index starts at 5
$s2 = gmp_init("01000001111", 2);
 public static function is_prime($n)
 {
     if (extension_loaded('gmp') && USE_EXT == 'GMP') {
         return gmp_prob_prime($n);
     } elseif (extension_loaded('bcmath') && USE_EXT == 'BCMATH') {
         self::$miller_rabin_test_count = 0;
         $t = 40;
         $k = 0;
         $m = bcsub($n, 1);
         while (bcmod($m, 2) == 0) {
             $k = bcadd($k, 1);
             $m = bcdiv($m, 2);
         }
         for ($i = 0; $i < $t; $i++) {
             $a = bcmath_Utils::bcrand(1, bcsub($n, 1));
             $b0 = self::modular_exp($a, $m, $n);
             if ($b0 != 1 && $b0 != bcsub($n, 1)) {
                 $j = 1;
                 while ($j <= $k - 1 && $b0 != bcsub($n, 1)) {
                     $b0 = self::modular_exp($b0, 2, $n);
                     if ($b0 == 1) {
                         self::$miller_rabin_test_count = $i + 1;
                         return false;
                     }
                     $j++;
                 }
                 if ($b0 != bcsub($n, 1)) {
                     self::$miller_rabin_test_count = $i + 1;
                     return false;
                 }
             }
         }
         return true;
     } else {
         throw new ErrorException("Please install BCMATH or GMP");
     }
 }
Пример #8
0
<?php

var_dump(gmp_prob_prime(10));
var_dump(gmp_prob_prime("7"));
var_dump(gmp_prob_prime(17));
var_dump(gmp_prob_prime(-31));
var_dump(gmp_prob_prime("172368715471481723"));
var_dump(gmp_prob_prime(10));
var_dump(gmp_prob_prime("7"));
var_dump(gmp_prob_prime(17));
var_dump(gmp_prob_prime(-31));
var_dump(gmp_prob_prime("172368715471481723"));
for ($i = -1; $i < 12; $i++) {
    var_dump(gmp_prob_prime(773 * $i - $i * 7 - 1, $i));
    $n = gmp_init("23476812735411");
    var_dump(gmp_prob_prime(gmp_add($n, $i - 1), $i));
}
$n = gmp_init("19481923");
var_dump(gmp_prob_prime($n));
$n = gmp_init(0);
var_dump(gmp_prob_prime($n));
var_dump(gmp_prob_prime());
var_dump(gmp_prob_prime(array()));
echo "Done\n";
Пример #9
0
 public function isPrime($t = false)
 {
     $length = strlen($this->toBytes());
     if (!$t) {
         if (163 <= $length) {
             $t = 2;
         } else {
             if (106 <= $length) {
                 $t = 3;
             } else {
                 if (81 <= $length) {
                     $t = 4;
                 } else {
                     if (68 <= $length) {
                         $t = 5;
                     } else {
                         if (56 <= $length) {
                             $t = 6;
                         } else {
                             if (50 <= $length) {
                                 $t = 7;
                             } else {
                                 if (43 <= $length) {
                                     $t = 8;
                                 } else {
                                     if (37 <= $length) {
                                         $t = 9;
                                     } else {
                                         if (31 <= $length) {
                                             $t = 12;
                                         } else {
                                             if (25 <= $length) {
                                                 $t = 15;
                                             } else {
                                                 if (18 <= $length) {
                                                     $t = 18;
                                                 } else {
                                                     $t = 27;
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     switch (MATH_BIGINTEGER_MODE) {
         case MATH_BIGINTEGER_MODE_GMP:
             return gmp_prob_prime($this->value, $t) != 0;
         case MATH_BIGINTEGER_MODE_BCMATH:
             if ($this->value === '2') {
                 return true;
             }
             if ($this->value[strlen($this->value) - 1] % 2 == 0) {
                 return false;
             }
             break;
         default:
             if ($this->value == array(2)) {
                 return true;
             }
             if (~$this->value[0] & 1) {
                 return false;
             }
     }
     static $primes;
     static $zero;
     static $one;
     static $two;
     if (!isset($primes)) {
         $primes = array(3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997);
         if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) {
             for ($i = 0; $i < count($primes); ++$i) {
                 $primes[$i] = new Math_BigInteger($primes[$i]);
             }
         }
         $zero = new Math_BigInteger();
         $one = new Math_BigInteger(1);
         $two = new Math_BigInteger(2);
     }
     if ($this->equals($one)) {
         return false;
     }
     if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) {
         foreach ($primes as $prime) {
             list(, $r) = $this->divide($prime);
             if ($r->equals($zero)) {
                 return $this->equals($prime);
             }
         }
     } else {
         $value = $this->value;
         foreach ($primes as $prime) {
             list(, $r) = $this->_divide_digit($value, $prime);
             if (!$r) {
                 return count($value) == 1 && $value[0] == $prime;
             }
         }
     }
     $n = $this->copy();
     $n_1 = $n->subtract($one);
     $n_2 = $n->subtract($two);
     $r = $n_1->copy();
     $r_value = $r->value;
     if (MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH) {
         $s = 0;
         while ($r->value[strlen($r->value) - 1] % 2 == 0) {
             $r->value = bcdiv($r->value, '2', 0);
             ++$s;
         }
     } else {
         $i = 0;
         for ($r_length = count($r_value); $i < $r_length; ++$i) {
             $temp = ~$r_value[$i] & 16777215;
             for ($j = 1; $temp >> $j & 1; ++$j) {
             }
             if ($j != 25) {
                 break;
             }
         }
         $s = 26 * $i + $j - 1;
         $r->_rshift($s);
     }
     for ($i = 0; $i < $t; ++$i) {
         $a = $this->random($two, $n_2);
         $y = $a->modPow($r, $n);
         if (!$y->equals($one) && !$y->equals($n_1)) {
             for ($j = 1; !$y->equals($n_1); ++$j) {
                 $y = $y->modPow($two, $n);
                 if ($y->equals($one)) {
                     return false;
                 }
             }
             if (!$y->equals($n_1)) {
                 return false;
             }
         }
     }
     return true;
 }
Пример #10
0
 /**
  * Generates prime number with length $bits_cnt
  * using $random_generator as random generator function.
  *
  * @param int $bits_cnt
  * @param string $rnd_generator
  * @access public
  */
 function getPrime($bits_cnt, $random_generator)
 {
     $bytes_n = intval($bits_cnt / 8);
     $bits_n = $bits_cnt % 8;
     do {
         $str = '';
         for ($i = 0; $i < $bytes_n; $i++) {
             $str .= chr(call_user_func($random_generator) & 0xff);
         }
         $n = call_user_func($random_generator) & 0xff;
         $n |= 0x80;
         $n >>= 8 - $bits_n;
         $str .= chr($n);
         $num = $this->bin2int($str);
         // search for the next closest prime number after [$num]
         if (!gmp_cmp(gmp_mod($num, '2'), '0')) {
             $num = gmp_add($num, '1');
         }
         while (!gmp_prob_prime($num)) {
             $num = gmp_add($num, '2');
         }
     } while ($this->bitLen($num) != $bits_cnt);
     return $num;
 }
Пример #11
0
 public function isPrime($number)
 {
     return gmp_prob_prime($number) ? true : false;
 }
Пример #12
0
 /**
  * @param int $number
  */
 public static function isPrime($number)
 {
     // Uses Miller-Rabin's probabilistic test
     // https://en.wikipedia.org/wiki/Miller-Rabin_primality_test
     return gmp_prob_prime($number) !== 0;
 }
Пример #13
0
 /**
  * {@inheritDoc}
  * @see \Mdanter\Ecc\MathAdapterInterface::isPrime()
  */
 public function isPrime($n)
 {
     $prob = gmp_prob_prime(gmp_init($n, 10));
     if ($prob > 0) {
         return true;
     }
     return false;
 }
# numero_primo
<?php 
//exemplo com o numero 10.
$numero = 10;
if (gmp_prob_prime($numero)) {
    print 'É primo';
} else {
    print 'Não é primo';
}
Пример #15
0
<?php

/**
 *
 * @Author: Marcelo C Menezes
 * https://github.com/marcelocmenezes
 * 
 * Etapa 1
 *
 */
$contador = 0;
for ($i = 2; $i <= 1000000; $i++) {
    if (gmp_prob_prime($i) == 2) {
        $contador++;
        if ($contador == 10001) {
            echo $i;
        }
    }
}
Пример #16
0
<?php

$i = 600851475143.0;
$x = 1;
while ($x <= $i) {
    if ($i % $x == 0) {
        $factor = $i / $x;
        if (gmp_prob_prime($factor) == 2) {
            echo $factor;
            break;
        }
    }
    $x++;
}