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
8a79c9f729
commit
b23b3b6262
@ -16,11 +16,12 @@ namespace danog;
|
||||
|
||||
class PrimeModule
|
||||
{
|
||||
|
||||
// Uses https://github.com/LonamiWebs/Telethon/blob/master/telethon/crypto/factorizator.py, thank you so freaking much!
|
||||
public static function native_single($what)
|
||||
{
|
||||
if (!is_int($what)) return false;
|
||||
if (!is_int($what)) {
|
||||
return false;
|
||||
}
|
||||
foreach ([2, 3, 5, 7, 11, 13, 17, 19, 23] as $s) {
|
||||
if ($what % $s === 0) {
|
||||
return $s;
|
||||
@ -63,8 +64,10 @@ class PrimeModule
|
||||
}
|
||||
}
|
||||
$p = $what;
|
||||
|
||||
return min($p, $g);
|
||||
}
|
||||
|
||||
private static function gcd($a, $b)
|
||||
{
|
||||
if ($a == $b) {
|
||||
@ -73,38 +76,59 @@ class PrimeModule
|
||||
while ($b > 0) {
|
||||
list($a, $b) = [$b, self::posmod($a, $b)];
|
||||
}
|
||||
|
||||
return $a;
|
||||
}
|
||||
private static function posmod($a, $b) {
|
||||
|
||||
private static function posmod($a, $b)
|
||||
{
|
||||
$resto = $a % $b;
|
||||
if ($resto < 0) {
|
||||
$resto += abs($b);
|
||||
}
|
||||
|
||||
return $resto;
|
||||
}
|
||||
|
||||
|
||||
public static function python_single($what) {
|
||||
public static function python_single($what)
|
||||
{
|
||||
if (function_exists('shell_exec')) {
|
||||
$res = shell_exec('timeout 10 python '.__DIR__.'/prime.py '.$what);
|
||||
if ($res == '' || is_null($res)) return false;
|
||||
$newval = intval($res);
|
||||
if (is_int($newval)) $res = $newval;
|
||||
if ($res === 0) return false;
|
||||
return $res;
|
||||
}
|
||||
if ($res == '' || is_null($res)) {
|
||||
return false;
|
||||
}
|
||||
public static function python_single_alt($what) {
|
||||
if (function_exists('shell_exec')) {
|
||||
$res = shell_exec('python '.__DIR__.'/alt_prime.py '.$what);
|
||||
if ($res == '' || is_null($res)) return false;
|
||||
$newval = intval($res);
|
||||
if (is_int($newval)) $res = $newval;
|
||||
if ($res === 0) return false;
|
||||
if (is_int($newval)) {
|
||||
$res = $newval;
|
||||
}
|
||||
if ($res === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function python_single_alt($what)
|
||||
{
|
||||
if (function_exists('shell_exec')) {
|
||||
$res = shell_exec('python '.__DIR__.'/alt_prime.py '.$what);
|
||||
if ($res == '' || is_null($res)) {
|
||||
return false;
|
||||
}
|
||||
$newval = intval($res);
|
||||
if (is_int($newval)) {
|
||||
$res = $newval;
|
||||
}
|
||||
if ($res === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -129,7 +153,9 @@ class PrimeModule
|
||||
$res = json_decode(curl_exec($ch), true);
|
||||
curl_close($ch);
|
||||
$fres = false;
|
||||
if (!isset($res['queryresult']['pods'])) return false;
|
||||
if (!isset($res['queryresult']['pods'])) {
|
||||
return false;
|
||||
}
|
||||
foreach ($res['queryresult']['pods'] as $cur) {
|
||||
if ($cur['id'] === 'Divisors') {
|
||||
$fres = explode(', ', preg_replace(["/{\d+, /", "/, \d+}$/"], '', $cur['subpods'][0]['moutput']));
|
||||
@ -140,62 +166,103 @@ class PrimeModule
|
||||
$fres = $fres[0];
|
||||
|
||||
$newval = intval($fres);
|
||||
if (is_int($newval)) $fres = $newval;
|
||||
if (is_int($newval)) {
|
||||
$fres = $newval;
|
||||
}
|
||||
|
||||
return $fres;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function native($what) {
|
||||
public static function native($what)
|
||||
{
|
||||
$res = [self::native_single($what)];
|
||||
while (array_product($res) !== $what) {
|
||||
$res[] = self::native_single($what / array_product($res));
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
public static function python($what) {
|
||||
|
||||
public static function python($what)
|
||||
{
|
||||
$res = [self::python_single($what)];
|
||||
if ($res[0] === false) return false;
|
||||
if ($res[0] === false) {
|
||||
return false;
|
||||
}
|
||||
while (array_product($res) !== $what) {
|
||||
$res[] = self::python_single($what / array_product($res));
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
public static function python_alt($what) {
|
||||
|
||||
public static function python_alt($what)
|
||||
{
|
||||
$res = [self::python_single_alt($what)];
|
||||
if ($res[0] === false) return false;
|
||||
if ($res[0] === false) {
|
||||
return false;
|
||||
}
|
||||
while (array_product($res) !== $what) {
|
||||
$res[] = self::python_single_alt($what / array_product($res));
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
public static function wolfram($what) {
|
||||
|
||||
public static function wolfram($what)
|
||||
{
|
||||
$res = [self::wolfram_single($what)];
|
||||
while (array_product($res) !== $what) {
|
||||
$res[] = self::wolfram_single($what / array_product($res));
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
public static function auto($what) {
|
||||
|
||||
public static function auto($what)
|
||||
{
|
||||
$res = self::python_alt($what);
|
||||
if (is_array($res)) return $res;
|
||||
if (is_array($res)) {
|
||||
return $res;
|
||||
}
|
||||
$res = self::python($what);
|
||||
if (is_array($res)) return $res;
|
||||
if (is_array($res)) {
|
||||
return $res;
|
||||
}
|
||||
$res = self::native((int) $what);
|
||||
if (is_array($res)) return $res;
|
||||
if (is_array($res)) {
|
||||
return $res;
|
||||
}
|
||||
$res = self::wolfram($what);
|
||||
if (is_array($res)) return $res;
|
||||
if (is_array($res)) {
|
||||
return $res;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
public static function auto_single($what) {
|
||||
|
||||
public static function auto_single($what)
|
||||
{
|
||||
$res = self::python_single_alt($what);
|
||||
if ($res !== false) return $res;
|
||||
if ($res !== false) {
|
||||
return $res;
|
||||
}
|
||||
$res = self::python_single($what);
|
||||
if ($res !== false) return $res;
|
||||
if ($res !== false) {
|
||||
return $res;
|
||||
}
|
||||
$res = self::native_single((int) $what);
|
||||
if ($res !== false) return $res;
|
||||
if ($res !== false) {
|
||||
return $res;
|
||||
}
|
||||
$res = self::wolfram_single($what);
|
||||
if ($res !== false) return $res;
|
||||
if ($res !== false) {
|
||||
return $res;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -2,75 +2,83 @@
|
||||
<?php
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
function get_time($callable, $param) {
|
||||
function get_time($callable, $param)
|
||||
{
|
||||
$a = microtime(true);
|
||||
$result = $callable($param);
|
||||
$time = microtime(true) - $a;
|
||||
if (is_array($result)) $result = json_encode($result);
|
||||
if (is_array($result)) {
|
||||
$result = json_encode($result);
|
||||
}
|
||||
|
||||
return [$time, $result];
|
||||
}
|
||||
function test($n) {
|
||||
$init = '| '.str_pad('result', strlen($n), " ", STR_PAD_RIGHT).' | type | time |';
|
||||
echo '|'.str_pad('', strlen($init)-2, "-", STR_PAD_RIGHT).'|'.PHP_EOL;
|
||||
function test($n)
|
||||
{
|
||||
$init = '| '.str_pad('result', strlen($n), ' ', STR_PAD_RIGHT).' | type | time |';
|
||||
echo '|'.str_pad('', strlen($init) - 2, '-', STR_PAD_RIGHT).'|'.PHP_EOL;
|
||||
|
||||
echo '|'.str_pad('Multiple factorization of '.$n, strlen($init)-2, " ", STR_PAD_BOTH).'|'.PHP_EOL;
|
||||
echo '|'.str_pad('', strlen($init)-2, "_", STR_PAD_RIGHT).'|'.PHP_EOL;
|
||||
echo '|'.str_pad('Multiple factorization of '.$n, strlen($init) - 2, ' ', STR_PAD_BOTH).'|'.PHP_EOL;
|
||||
echo '|'.str_pad('', strlen($init) - 2, '_', STR_PAD_RIGHT).'|'.PHP_EOL;
|
||||
echo $init.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;
|
||||
echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | python alt | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
list($time, $result) = get_time(['\danog\PrimeModule', 'python'], $n);
|
||||
$GLOBALS['medium']['python'] += $time;
|
||||
echo '| '.str_pad($result, 6, " ", STR_PAD_RIGHT).' | wolfram | '.str_pad($time, 20, " ", STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | wolfram | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
list($time, $result) = get_time(['\danog\PrimeModule', 'native'], $n);
|
||||
$GLOBALS['medium']['native'] += $time;
|
||||
echo '| '.str_pad($result, 6, " ", STR_PAD_RIGHT).' | python | '.str_pad($time, 20, " ", STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | python | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
list($time, $result) = get_time(['\danog\PrimeModule', 'wolfram'], $n);
|
||||
$GLOBALS['medium']['wolfram'] += $time;
|
||||
echo '| '.str_pad($result, 6, " ", STR_PAD_RIGHT).' | native | '.str_pad($time, 20, " ", STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
echo '| '.str_pad($result, 6, ' ', STR_PAD_RIGHT).' | native | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
//echo '| '.str_pad($n, 6, " ", STR_PAD_RIGHT).' | auto | '.str_pad(get_time(['\danog\PrimeModule', 'auto'], $n), 20, " ", STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
echo '|'.str_pad('', strlen($init)-2, "-", STR_PAD_RIGHT).'|'.PHP_EOL.PHP_EOL;
|
||||
|
||||
echo '|'.str_pad('', strlen($init) - 2, '-', STR_PAD_RIGHT).'|'.PHP_EOL.PHP_EOL;
|
||||
}
|
||||
function test_single($n, $messy = false) {
|
||||
$init = '| '.str_pad('result', strlen($n), " ", STR_PAD_RIGHT).' | type | time |';
|
||||
echo '|'.str_pad('', strlen($init)-2, "-", STR_PAD_RIGHT).'|'.PHP_EOL;
|
||||
echo '|'.str_pad('Single factorization of '.$n, strlen($init)-2, " ", STR_PAD_BOTH).'|'.PHP_EOL;
|
||||
echo '|'.str_pad('', strlen($init)-2, "_", STR_PAD_RIGHT).'|'.PHP_EOL;
|
||||
function test_single($n, $messy = false)
|
||||
{
|
||||
$init = '| '.str_pad('result', strlen($n), ' ', STR_PAD_RIGHT).' | type | time |';
|
||||
echo '|'.str_pad('', strlen($init) - 2, '-', STR_PAD_RIGHT).'|'.PHP_EOL;
|
||||
echo '|'.str_pad('Single factorization of '.$n, strlen($init) - 2, ' ', STR_PAD_BOTH).'|'.PHP_EOL;
|
||||
echo '|'.str_pad('', strlen($init) - 2, '_', STR_PAD_RIGHT).'|'.PHP_EOL;
|
||||
echo $init.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;
|
||||
echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | python alt | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
if (!$messy) {
|
||||
list($time, $result) = get_time(['\danog\PrimeModule', 'python_single'], $n);
|
||||
$GLOBALS['medium']['python'] += $time;
|
||||
echo '| '.str_pad($result, strlen($n), " ", STR_PAD_RIGHT).' | python | '.str_pad($time, 20, " ", STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | python | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
}
|
||||
|
||||
list($time, $result) = get_time(['\danog\PrimeModule', 'native_single'], $n);
|
||||
$GLOBALS['medium']['native'] += $time;
|
||||
echo '| '.str_pad($result, strlen($n), " ", STR_PAD_RIGHT).' | native | '.str_pad($time, 20, " ", STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | native | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
list($time, $result) = get_time(['\danog\PrimeModule', 'wolfram_single'], $n);
|
||||
$GLOBALS['medium']['wolfram'] += $time;
|
||||
echo '| '.str_pad($result, strlen($n), " ", STR_PAD_RIGHT).' | wolfram | '.str_pad($time, 20, " ", STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
|
||||
echo '|'.str_pad('', strlen($init)-2, "-", STR_PAD_RIGHT).'|'.PHP_EOL.PHP_EOL;
|
||||
echo '| '.str_pad($result, strlen($n), ' ', STR_PAD_RIGHT).' | wolfram | '.str_pad($time, 20, ' ', STR_PAD_RIGHT).' |'.PHP_EOL;
|
||||
|
||||
echo '|'.str_pad('', strlen($init) - 2, '-', STR_PAD_RIGHT).'|'.PHP_EOL.PHP_EOL;
|
||||
}
|
||||
function random_string($length)
|
||||
{
|
||||
if (function_exists('random_bytes')) {
|
||||
return random_bytes($length);
|
||||
}
|
||||
function random_string($length) {
|
||||
if (function_exists('random_bytes')) return random_bytes($length);
|
||||
$s = '';
|
||||
for ($x = 0; $x < $length; $x++) {
|
||||
$s .= chr(rand(0, 255));
|
||||
}
|
||||
|
||||
return $s;
|
||||
}
|
||||
function gen_payload() {
|
||||
function gen_payload()
|
||||
{
|
||||
return pack('CPPVV', 10, 0, (int) (time() << 32), 20, 1615239032).random_string(16);
|
||||
}
|
||||
|
||||
|
||||
echo PHP_EOL.'----------- HUGE SEMIPRIME TESTS (100 semiprimes) ----------'.PHP_EOL;
|
||||
$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0];
|
||||
$tg = fsockopen('tcp://149.154.167.40:443');
|
||||
@ -106,7 +114,6 @@ foreach ($medium as $type => $total) {
|
||||
echo $type.': total time '.$total.', medium time '.($total / 4).PHP_EOL;
|
||||
}
|
||||
|
||||
|
||||
echo PHP_EOL.'------------------- HUGE SEMIPRIME TESTS (MESSY) ------------------'.PHP_EOL;
|
||||
$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0];
|
||||
$m = [1724114033281923457, 2189285106422392999, 3510535493073971807, 1756377470921216651, 1767867620107504387, 2149465210997855797];
|
||||
|
Loading…
Reference in New Issue
Block a user