mirror of
https://github.com/danog/PrimeModule.git
synced 2024-11-26 20:34:37 +01:00
Apply fixes from StyleCI
This commit is contained in:
parent
3e168a125f
commit
c15f348b68
@ -265,30 +265,30 @@ class PrimeModule
|
|||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private function primesbelow($N) {
|
private function primesbelow($N)
|
||||||
|
{
|
||||||
$correction = ($N % 6 > 1) ? true : false;
|
$correction = ($N % 6 > 1) ? true : false;
|
||||||
$N_Array = [$N, $N-1, $N+4, $N+3, $N+2, $N+1];
|
$N_Array = [$N, $N - 1, $N + 4, $N + 3, $N + 2, $N + 1];
|
||||||
$N = $N_Array[$N%6];
|
$N = $N_Array[$N % 6];
|
||||||
|
|
||||||
$sieve = array();
|
$sieve = [];
|
||||||
for ($i=0; $i < (int)$N/3 ; $i++) {
|
for ($i = 0; $i < (int) $N / 3; $i++) {
|
||||||
$sieve[$i] = true;
|
$sieve[$i] = true;
|
||||||
}
|
}
|
||||||
$sieve[0] = false;
|
$sieve[0] = false;
|
||||||
|
|
||||||
for ($i=0; $i < (int)((int)pow($N, 0.5) / 3) + 1; $i++) {
|
for ($i = 0; $i < (int) ((int) pow($N, 0.5) / 3) + 1; $i++) {
|
||||||
if ($sieve[$i]) {
|
if ($sieve[$i]) {
|
||||||
$k = (3 * $i + 1) | 1;
|
$k = (3 * $i + 1) | 1;
|
||||||
$startIndex1 = (int)($k*$k / 3);
|
$startIndex1 = (int) ($k * $k / 3);
|
||||||
$period = 2*$k;
|
$period = 2 * $k;
|
||||||
for ($j=$startIndex1; $j < count($sieve); $j = $j+$period) {
|
for ($j = $startIndex1; $j < count($sieve); $j = $j + $period) {
|
||||||
$sieve[$j] = false;
|
$sieve[$j] = false;
|
||||||
}
|
}
|
||||||
$startIndex2 = (int)( ($k*$k + 4*$k - 2*$k*($i%2)) /3 );
|
$startIndex2 = (int) (($k * $k + 4 * $k - 2 * $k * ($i % 2)) / 3);
|
||||||
$period = 2*$k;
|
$period = 2 * $k;
|
||||||
for ($k=$startIndex2; $k < count($sieve); $k = $k+$period) {
|
for ($k = $startIndex2; $k < count($sieve); $k = $k + $period) {
|
||||||
$sieve[$k] = false;
|
$sieve[$k] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -296,50 +296,53 @@ class PrimeModule
|
|||||||
|
|
||||||
$resultArray = [2, 3];
|
$resultArray = [2, 3];
|
||||||
$t = 1;
|
$t = 1;
|
||||||
for ($i=1; $i < (int)($N/3) - $correction ; $i++) {
|
for ($i = 1; $i < (int) ($N / 3) - $correction; $i++) {
|
||||||
if ($sieve[$i]) {
|
if ($sieve[$i]) {
|
||||||
$resultArray[$t+1] = (3 * $i + 1) | 1;
|
$resultArray[$t + 1] = (3 * $i + 1) | 1;
|
||||||
$t++;
|
$t++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $resultArray;
|
return $resultArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function isprime ($n, $precision = 7) {
|
private function isprime($n, $precision = 7)
|
||||||
$smallprimeset = $this->primesbelow(100000);
|
{
|
||||||
|
$smallprimeset = $this->primesbelow(100000);
|
||||||
$_smallprimeset = 100000;
|
$_smallprimeset = 100000;
|
||||||
if ($n == 1 || $n % 2 == 0) {
|
if ($n == 1 || $n % 2 == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
} elseif ($n < 1) {
|
||||||
elseif ($n < 1)
|
throw new Exception('Out of bounds, first argument must be > 0');
|
||||||
throw new Exception("Out of bounds, first argument must be > 0");
|
} elseif ($n < $_smallprimeset) {
|
||||||
elseif ($n < $_smallprimeset) {
|
|
||||||
return in_array($n, $smallprimeset);
|
return in_array($n, $smallprimeset);
|
||||||
}
|
}
|
||||||
|
|
||||||
$d = $n - 1;
|
$d = $n - 1;
|
||||||
$s = 0;
|
$s = 0;
|
||||||
while ($d % 2 == 0){
|
while ($d % 2 == 0) {
|
||||||
$d = (int)($d / 2);
|
$d = (int) ($d / 2);
|
||||||
$s += 1;
|
$s += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
for ($i=0; $i < $precision; $i++) {
|
for ($i = 0; $i < $precision; $i++) {
|
||||||
// random.randrange(2, n - 2) means:
|
// random.randrange(2, n - 2) means:
|
||||||
// $a = mt_rand(2, $n - 3);
|
// $a = mt_rand(2, $n - 3);
|
||||||
// maybe $n would be bigger that PHP_MAX_INT
|
// maybe $n would be bigger that PHP_MAX_INT
|
||||||
$a = mt_rand(2, 1084);
|
$a = mt_rand(2, 1084);
|
||||||
$x = bcpowmod($a, $d, $n);
|
$x = bcpowmod($a, $d, $n);
|
||||||
|
|
||||||
if ($x == 1 || $x == $n - 1) continue;
|
if ($x == 1 || $x == $n - 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$flagfound = 0;
|
$flagfound = 0;
|
||||||
for ($j=0; $j < $s - 1; $j++) {
|
for ($j = 0; $j < $s - 1; $j++) {
|
||||||
$x = bcpowmod($x, 2, $n);
|
$x = bcpowmod($x, 2, $n);
|
||||||
if ($x == 1){
|
if ($x == 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ($x == $n - 1){
|
if ($x == $n - 1) {
|
||||||
$flagfound = 1;
|
$flagfound = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -348,17 +351,24 @@ class PrimeModule
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function pollard_brent($n) {
|
private function pollard_brent($n)
|
||||||
|
{
|
||||||
|
$n = (int) $n;
|
||||||
|
|
||||||
$n = (int)$n;
|
if ($n % 2 == 0) {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
if ($n % 2 == 0) return 2;
|
if (bcmod($n, 2) == 0) {
|
||||||
|
return 2;
|
||||||
if ( bcmod($n , 2) == 0 ) return 2;
|
}
|
||||||
if ( bcmod($n , 3) == 0 ) return 3;
|
if (bcmod($n, 3) == 0) {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
// $y = mt_rand(1, $n-1);
|
// $y = mt_rand(1, $n-1);
|
||||||
// $c = mt_rand(1, $n-1);
|
// $c = mt_rand(1, $n-1);
|
||||||
@ -375,21 +385,21 @@ class PrimeModule
|
|||||||
|
|
||||||
while ($g == 1) {
|
while ($g == 1) {
|
||||||
$x = $y;
|
$x = $y;
|
||||||
for ($i=0; $i < $r; $i++) {
|
for ($i = 0; $i < $r; $i++) {
|
||||||
// $y = gmp_mod( (bcpowmod($y, 2, $n) + $c) , $n);
|
// $y = gmp_mod( (bcpowmod($y, 2, $n) + $c) , $n);
|
||||||
$y = bcmod( (bcpowmod($y, 2, $n) + $c) , $n);
|
$y = bcmod((bcpowmod($y, 2, $n) + $c), $n);
|
||||||
}
|
}
|
||||||
|
|
||||||
$k = 0;
|
$k = 0;
|
||||||
while ($k < $r && $g == 1) {
|
while ($k < $r && $g == 1) {
|
||||||
$ys= $y;
|
$ys = $y;
|
||||||
for ($j=0; $j < min($m, $r-$k); $j++) {
|
for ($j = 0; $j < min($m, $r - $k); $j++) {
|
||||||
|
|
||||||
// $y = gmp_mod( (bcpowmod($y, 2, $n) + $c), $n );
|
// $y = gmp_mod( (bcpowmod($y, 2, $n) + $c), $n );
|
||||||
$y = bcmod( (bcpowmod($y, 2, $n) + $c), $n );
|
$y = bcmod((bcpowmod($y, 2, $n) + $c), $n);
|
||||||
|
|
||||||
// $q = gmp_mod($q * abs($x-$y), $n);
|
// $q = gmp_mod($q * abs($x-$y), $n);
|
||||||
$mul = bcmul($q, abs($x-$y));
|
$mul = bcmul($q, abs($x - $y));
|
||||||
$q = bcmod($mul, $n);
|
$q = bcmod($mul, $n);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -401,32 +411,32 @@ class PrimeModule
|
|||||||
if ($g == $n) {
|
if ($g == $n) {
|
||||||
while (true) {
|
while (true) {
|
||||||
// $ys = ( bcpowmod($ys, 2, $n) + $c ) % $n;
|
// $ys = ( bcpowmod($ys, 2, $n) + $c ) % $n;
|
||||||
$ys = bcmod (( bcpowmod($ys, 2, $n) + $c ) , $n);
|
$ys = bcmod((bcpowmod($ys, 2, $n) + $c), $n);
|
||||||
$g = $this->gcd2( abs($x - $ys), $n );
|
$g = $this->gcd2(abs($x - $ys), $n);
|
||||||
if ($g > 1) {
|
if ($g > 1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $g;
|
return $g;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function primefactors($n, $sort = false)
|
||||||
public function primefactors($n, $sort = false) {
|
{
|
||||||
|
|
||||||
$smallprimes = $this->primesbelow(10000);
|
$smallprimes = $this->primesbelow(10000);
|
||||||
$factors = [];
|
$factors = [];
|
||||||
|
|
||||||
$limit = bcadd( bcsqrt($n) , 1);
|
$limit = bcadd(bcsqrt($n), 1);
|
||||||
foreach ($smallprimes as $checker) {
|
foreach ($smallprimes as $checker) {
|
||||||
if ($checker > $limit) {
|
if ($checker > $limit) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// while (gmp_mod($n, $checker) == 0) {
|
// while (gmp_mod($n, $checker) == 0) {
|
||||||
while ( bcmod($n, $checker) == 0 ) {
|
while (bcmod($n, $checker) == 0) {
|
||||||
array_push($factors, $checker);
|
array_push($factors, $checker);
|
||||||
$n = bcdiv($n, $checker);
|
$n = bcdiv($n, $checker);
|
||||||
$limit = bcadd( bcsqrt($n) , 1);
|
$limit = bcadd(bcsqrt($n), 1);
|
||||||
if ($checker > $limit) {
|
if ($checker > $limit) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -442,29 +452,28 @@ class PrimeModule
|
|||||||
array_push($factors, $n);
|
array_push($factors, $n);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
$factor = $this->pollard_brent($n);
|
$factor = $this->pollard_brent($n);
|
||||||
$factors = array_merge($factors, $this->primefactors($factor));
|
$factors = array_merge($factors, $this->primefactors($factor));
|
||||||
$n = (int)($n/$factor);
|
$n = (int) ($n / $factor);
|
||||||
}
|
}
|
||||||
if ($sort) {
|
if ($sort) {
|
||||||
sort($factors);
|
sort($factors);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $factors;
|
return $factors;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function gcd2($a, $b)
|
||||||
private function gcd2($a, $b) {
|
{
|
||||||
if ($a == $b) return $a;
|
if ($a == $b) {
|
||||||
|
return $a;
|
||||||
|
}
|
||||||
while ($b > 0) {
|
while ($b > 0) {
|
||||||
$a2 = $a;
|
$a2 = $a;
|
||||||
$a = $b;
|
$a = $b;
|
||||||
$b = bcmod($a2 , $b);
|
$b = bcmod($a2, $b);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $a;
|
return $a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require '../lib/danog/PrimeModule.php';
|
require '../lib/danog/PrimeModule.php';
|
||||||
$factorization = new danog\PrimeModule();
|
$factorization = new danog\PrimeModule();
|
||||||
$res = $factorization->primefactors("1278426847636566097");
|
$res = $factorization->primefactors('1278426847636566097');
|
||||||
print_r($res);
|
print_r($res);
|
||||||
?>
|
|
Loading…
Reference in New Issue
Block a user