1
0
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:
Daniil Gentili 2017-02-21 19:37:13 +00:00 committed by StyleCI Bot
parent 8a79c9f729
commit b23b3b6262
2 changed files with 246 additions and 172 deletions

View File

@ -16,11 +16,12 @@ namespace danog;
class PrimeModule class PrimeModule
{ {
// Uses https://github.com/LonamiWebs/Telethon/blob/master/telethon/crypto/factorizator.py, thank you so freaking much! // Uses https://github.com/LonamiWebs/Telethon/blob/master/telethon/crypto/factorizator.py, thank you so freaking much!
public static function native_single($what) 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) { foreach ([2, 3, 5, 7, 11, 13, 17, 19, 23] as $s) {
if ($what % $s === 0) { if ($what % $s === 0) {
return $s; return $s;
@ -63,8 +64,10 @@ class PrimeModule
} }
} }
$p = $what; $p = $what;
return min($p, $g); return min($p, $g);
} }
private static function gcd($a, $b) private static function gcd($a, $b)
{ {
if ($a == $b) { if ($a == $b) {
@ -73,38 +76,59 @@ class PrimeModule
while ($b > 0) { while ($b > 0) {
list($a, $b) = [$b, self::posmod($a, $b)]; list($a, $b) = [$b, self::posmod($a, $b)];
} }
return $a; return $a;
} }
private static function posmod($a, $b) {
private static function posmod($a, $b)
{
$resto = $a % $b; $resto = $a % $b;
if ($resto < 0) { if ($resto < 0) {
$resto += abs($b); $resto += abs($b);
} }
return $resto; return $resto;
} }
public static function python_single($what)
public static function python_single($what) { {
if (function_exists('shell_exec')) { if (function_exists('shell_exec')) {
$res = shell_exec('timeout 10 python '.__DIR__.'/prime.py '.$what); $res = shell_exec('timeout 10 python '.__DIR__.'/prime.py '.$what);
if ($res == '' || is_null($res)) return false; if ($res == '' || is_null($res)) {
$newval = intval($res);
if (is_int($newval)) $res = $newval;
if ($res === 0) return false;
return $res;
}
return false; 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); $newval = intval($res);
if (is_int($newval)) $res = $newval; if (is_int($newval)) {
if ($res === 0) return false; $res = $newval;
}
if ($res === 0) {
return false;
}
return $res; 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; return false;
} }
@ -129,7 +153,9 @@ class PrimeModule
$res = json_decode(curl_exec($ch), true); $res = json_decode(curl_exec($ch), true);
curl_close($ch); curl_close($ch);
$fres = false; $fres = false;
if (!isset($res['queryresult']['pods'])) return false; if (!isset($res['queryresult']['pods'])) {
return false;
}
foreach ($res['queryresult']['pods'] as $cur) { foreach ($res['queryresult']['pods'] as $cur) {
if ($cur['id'] === 'Divisors') { if ($cur['id'] === 'Divisors') {
$fres = explode(', ', preg_replace(["/{\d+, /", "/, \d+}$/"], '', $cur['subpods'][0]['moutput'])); $fres = explode(', ', preg_replace(["/{\d+, /", "/, \d+}$/"], '', $cur['subpods'][0]['moutput']));
@ -140,62 +166,103 @@ class PrimeModule
$fres = $fres[0]; $fres = $fres[0];
$newval = intval($fres); $newval = intval($fres);
if (is_int($newval)) $fres = $newval; if (is_int($newval)) {
$fres = $newval;
}
return $fres; return $fres;
} }
return false; return false;
} }
public static function native($what) { public static function native($what)
{
$res = [self::native_single($what)]; $res = [self::native_single($what)];
while (array_product($res) !== $what) { while (array_product($res) !== $what) {
$res[] = self::native_single($what / array_product($res)); $res[] = self::native_single($what / array_product($res));
} }
return $res; return $res;
} }
public static function python($what) {
public static function python($what)
{
$res = [self::python_single($what)]; $res = [self::python_single($what)];
if ($res[0] === false) return false; if ($res[0] === false) {
return false;
}
while (array_product($res) !== $what) { while (array_product($res) !== $what) {
$res[] = self::python_single($what / array_product($res)); $res[] = self::python_single($what / array_product($res));
} }
return $res; return $res;
} }
public static function python_alt($what) {
public static function python_alt($what)
{
$res = [self::python_single_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) { while (array_product($res) !== $what) {
$res[] = self::python_single_alt($what / array_product($res)); $res[] = self::python_single_alt($what / array_product($res));
} }
return $res; return $res;
} }
public static function wolfram($what) {
public static function wolfram($what)
{
$res = [self::wolfram_single($what)]; $res = [self::wolfram_single($what)];
while (array_product($res) !== $what) { while (array_product($res) !== $what) {
$res[] = self::wolfram_single($what / array_product($res)); $res[] = self::wolfram_single($what / array_product($res));
} }
return $res; return $res;
} }
public static function auto($what) {
public static function auto($what)
{
$res = self::python_alt($what); $res = self::python_alt($what);
if (is_array($res)) return $res; if (is_array($res)) {
return $res;
}
$res = self::python($what); $res = self::python($what);
if (is_array($res)) return $res; if (is_array($res)) {
return $res;
}
$res = self::native((int) $what); $res = self::native((int) $what);
if (is_array($res)) return $res; if (is_array($res)) {
return $res;
}
$res = self::wolfram($what); $res = self::wolfram($what);
if (is_array($res)) return $res; if (is_array($res)) {
return $res;
}
return false; return false;
} }
public static function auto_single($what) {
public static function auto_single($what)
{
$res = self::python_single_alt($what); $res = self::python_single_alt($what);
if ($res !== false) return $res; if ($res !== false) {
return $res;
}
$res = self::python_single($what); $res = self::python_single($what);
if ($res !== false) return $res; if ($res !== false) {
return $res;
}
$res = self::native_single((int) $what); $res = self::native_single((int) $what);
if ($res !== false) return $res; if ($res !== false) {
return $res;
}
$res = self::wolfram_single($what); $res = self::wolfram_single($what);
if ($res !== false) return $res; if ($res !== false) {
return $res;
}
return false; return false;
} }
} }

View File

@ -2,75 +2,83 @@
<?php <?php
require 'vendor/autoload.php'; require 'vendor/autoload.php';
function get_time($callable, $param) { function get_time($callable, $param)
{
$a = microtime(true); $a = microtime(true);
$result = $callable($param); $result = $callable($param);
$time = microtime(true) - $a; $time = microtime(true) - $a;
if (is_array($result)) $result = json_encode($result); if (is_array($result)) {
$result = json_encode($result);
}
return [$time, $result]; return [$time, $result];
} }
function test($n) { 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; $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('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('', strlen($init) - 2, '_', STR_PAD_RIGHT).'|'.PHP_EOL;
echo $init.PHP_EOL; echo $init.PHP_EOL;
list($time, $result) = get_time(['\danog\PrimeModule', 'python_alt'], $n, true); list($time, $result) = get_time(['\danog\PrimeModule', 'python_alt'], $n, true);
$GLOBALS['medium']['python_alt'] += $time; $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); list($time, $result) = get_time(['\danog\PrimeModule', 'python'], $n);
$GLOBALS['medium']['python'] += $time; $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); list($time, $result) = get_time(['\danog\PrimeModule', 'native'], $n);
$GLOBALS['medium']['native'] += $time; $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); list($time, $result) = get_time(['\danog\PrimeModule', 'wolfram'], $n);
$GLOBALS['medium']['wolfram'] += $time; $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($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) { 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; $init = '| '.str_pad('result', strlen($n), ' ', STR_PAD_RIGHT).' | type | time |';
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 '|'.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; echo $init.PHP_EOL;
list($time, $result) = get_time(['\danog\PrimeModule', 'python_single_alt'], $n); list($time, $result) = get_time(['\danog\PrimeModule', 'python_single_alt'], $n);
$GLOBALS['medium']['python_alt'] += $time; $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) { if (!$messy) {
list($time, $result) = get_time(['\danog\PrimeModule', 'python_single'], $n); list($time, $result) = get_time(['\danog\PrimeModule', 'python_single'], $n);
$GLOBALS['medium']['python'] += $time; $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); list($time, $result) = get_time(['\danog\PrimeModule', 'native_single'], $n);
$GLOBALS['medium']['native'] += $time; $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); list($time, $result) = get_time(['\danog\PrimeModule', 'wolfram_single'], $n);
$GLOBALS['medium']['wolfram'] += $time; $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($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('', 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 = ''; $s = '';
for ($x = 0; $x < $length; $x++) { for ($x = 0; $x < $length; $x++) {
$s .= chr(rand(0, 255)); $s .= chr(rand(0, 255));
} }
return $s; return $s;
} }
function gen_payload() { function gen_payload()
{
return pack('CPPVV', 10, 0, (int) (time() << 32), 20, 1615239032).random_string(16); return pack('CPPVV', 10, 0, (int) (time() << 32), 20, 1615239032).random_string(16);
} }
echo PHP_EOL.'----------- HUGE SEMIPRIME TESTS (100 semiprimes) ----------'.PHP_EOL; echo PHP_EOL.'----------- HUGE SEMIPRIME TESTS (100 semiprimes) ----------'.PHP_EOL;
$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0]; $GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0];
$tg = fsockopen('tcp://149.154.167.40:443'); $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 $type.': total time '.$total.', medium time '.($total / 4).PHP_EOL;
} }
echo PHP_EOL.'------------------- HUGE SEMIPRIME TESTS (MESSY) ------------------'.PHP_EOL; 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];
$m = [1724114033281923457, 2189285106422392999, 3510535493073971807, 1756377470921216651, 1767867620107504387, 2149465210997855797]; $m = [1724114033281923457, 2189285106422392999, 3510535493073971807, 1756377470921216651, 1767867620107504387, 2149465210997855797];