diff --git a/lib/danog/PrimeModule.php b/lib/danog/PrimeModule.php index daba262..f233dc5 100644 --- a/lib/danog/PrimeModule.php +++ b/lib/danog/PrimeModule.php @@ -112,6 +112,33 @@ class PrimeModule return false; } + public static function factor_single($what) + { + if (function_exists('shell_exec')) { + $res = trim(shell_exec('timeout 10 factor '.$what.' 2>&1') ?? ''); + $res = explode(' ', $res); + if (count($res) !== 3) { + return false; + } + return (int) $res[1]; + } + + return false; + } + + public static function factor($what) + { + $res = [self::factor_single($what)]; + if ($res[0] === false) { + return false; + } + while (array_product($res) !== $what) { + $res[] = self::factor_single($what / array_product($res)); + } + + return $res; + } + public static function python($what) { $res = [self::python_single($what)]; diff --git a/tests/testing.php b/tests/testing.php index a7d8b68..c9456d0 100755 --- a/tests/testing.php +++ b/tests/testing.php @@ -28,6 +28,10 @@ function test($n) $GLOBALS['medium']['native_cpp'] += $time; echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | native cpp | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL; + list($time, $result) = get_time(['\danog\PrimeModule', 'factor'], $n, true); + $GLOBALS['medium']['factor'] += $time; + echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | factor | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL; + list($time, $result) = get_time(['\danog\PrimeModule', 'python_alt'], $n, true); $GLOBALS['medium']['python_alt'] += $time; echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | python alt | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL; @@ -58,6 +62,10 @@ function test_single($n, $messy = false) $GLOBALS['medium']['native_cpp'] += $time; echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | native cpp | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL; + list($time, $result) = get_time(['\danog\PrimeModule', 'factor_single'], $n); + $GLOBALS['medium']['factor'] += $time; + echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | factor | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL; + list($time, $result) = get_time(['\danog\PrimeModule', 'python_single_alt'], $n); $GLOBALS['medium']['python_alt'] += $time; echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | python alt | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL; @@ -95,7 +103,7 @@ function gen_payload() } echo PHP_EOL.'----------- HUGE SEMIPRIME TESTS (100 semiprimes) ----------'.PHP_EOL; -$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0, 'native_cpp' => 0]; +$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0, 'native_cpp' => 0, 'factor' => 0]; $tg = fsockopen('tcp://149.154.167.51:443'); fwrite($tg, chr(239)); stream_set_timeout($tg, 1); @@ -112,7 +120,7 @@ foreach ($medium as $type => $total) { } echo PHP_EOL.'------------------- SMALL MULTIPLE FACTOR TESTS -------------------'.PHP_EOL; -$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0, 'native_cpp' => 0]; +$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0, 'native_cpp' => 0, 'factor' => 0]; foreach ([200, 327, 35, 13589] as $multiple) { test($multiple); @@ -123,7 +131,7 @@ foreach ($medium as $type => $total) { } echo PHP_EOL.'------------------- HUGE SEMIPRIME TESTS (MESSY) ------------------'.PHP_EOL; -$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0]; +$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0, 'factor' => 0]; $m = [1724114033281923457, 2189285106422392999, 3510535493073971807, 1756377470921216651, 1767867620107504387, 2149465210997855797]; foreach ($m as $messy) { test_single($messy, true);