1
0
mirror of https://github.com/danog/PrimeModule.git synced 2024-11-30 04:39:09 +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,186 +16,253 @@ 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;
} }
} }
$g = 0; $g = 0;
for ($i = 0; $i < 3; $i++) { for ($i = 0; $i < 3; $i++) {
$q = (rand(0, 127) & 15) + 17; $q = (rand(0, 127) & 15) + 17;
$x = rand(0, 1000000000) + 1; $x = rand(0, 1000000000) + 1;
$y = $x; $y = $x;
$lim = 1 << ($i + 18); $lim = 1 << ($i + 18);
for ($j = 1; $j <= $lim; $j++) { for ($j = 1; $j <= $lim; $j++) {
list($a, $b, $c) = [$x, $x, $q]; list($a, $b, $c) = [$x, $x, $q];
while ($b != 0) { while ($b != 0) {
if (($b & 1) != 0) { if (($b & 1) != 0) {
$c += $a; $c += $a;
if ($c >= $what) { if ($c >= $what) {
$c -= $what; $c -= $what;
} }
} }
$a += $a; $a += $a;
if ($a >= $what) { if ($a >= $what) {
$a -= $what; $a -= $what;
} }
$b >>= 1; $b >>= 1;
} }
$x = $c; $x = $c;
$z = ($x < $y) ? $y - $x : $x - $y; $z = ($x < $y) ? $y - $x : $x - $y;
$g = self::gcd($z, $what); $g = self::gcd($z, $what);
if ($g != 1) { if ($g != 1) {
break; break;
} }
if (($j & ($j - 1)) === 0) {
$y = $x;
}
}
if ($g > 1) {
break;
}
}
$p = $what;
return min($p, $g);
}
private static function gcd($a, $b)
{
if ($a == $b) {
return $a;
}
while ($b > 0) {
list($a, $b) = [$b, self::posmod($a, $b)];
}
return $a;
}
private static function posmod($a, $b) {
if (($j & ($j - 1)) === 0) {
$y = $x;
}
}
if ($g > 1) {
break;
}
}
$p = $what;
return min($p, $g);
}
private static function gcd($a, $b)
{
if ($a == $b) {
return $a;
}
while ($b > 0) {
list($a, $b) = [$b, self::posmod($a, $b)];
}
return $a;
}
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)
{
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;
}
public static function python_single($what) { return $res;
if (function_exists('shell_exec')) { }
$res = shell_exec('timeout 10 python '.__DIR__.'/prime.py '.$what);
if ($res == '' || is_null($res)) return false; return false;
$newval = intval($res);
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;
}
public static function wolfram_single($what)
{
$query = 'Do prime factorization of '.$what;
$params = [
'async' => true,
'banners' => 'raw',
'debuggingdata' => false,
'format' => 'moutput',
'formattimeout' => 8,
'input' => $query,
'output' => 'JSON',
'proxycode' => json_decode(file_get_contents('http://www.wolframalpha.com/api/v1/code'), true)['code'],
];
$url = 'https://www.wolframalpha.com/input/json.jsp?'.http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Referer: https://www.wolframalpha.com/input/?i='.urlencode($query)]);
curl_setopt($ch, CURLOPT_URL, $url);
$res = json_decode(curl_exec($ch), true);
curl_close($ch);
$fres = 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']));
break;
}
}
if (is_array($fres)) {
$fres = $fres[0];
$newval = intval($fres);
if (is_int($newval)) $fres = $newval;
return $fres;
}
return false;
} }
public static function native($what) { 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;
}
public static function wolfram_single($what)
{
$query = 'Do prime factorization of '.$what;
$params = [
'async' => true,
'banners' => 'raw',
'debuggingdata' => false,
'format' => 'moutput',
'formattimeout' => 8,
'input' => $query,
'output' => 'JSON',
'proxycode' => json_decode(file_get_contents('http://www.wolframalpha.com/api/v1/code'), true)['code'],
];
$url = 'https://www.wolframalpha.com/input/json.jsp?'.http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Referer: https://www.wolframalpha.com/input/?i='.urlencode($query)]);
curl_setopt($ch, CURLOPT_URL, $url);
$res = json_decode(curl_exec($ch), true);
curl_close($ch);
$fres = 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']));
break;
}
}
if (is_array($fres)) {
$fres = $fres[0];
$newval = intval($fres);
if (is_int($newval)) {
$fres = $newval;
}
return $fres;
}
return false;
}
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) {
while (array_product($res) !== $what) { return false;
$res [] = self::python_single($what / array_product($res));
} }
while (array_product($res) !== $what) {
$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) {
while (array_product($res) !== $what) { return false;
$res [] = self::python_single_alt($what / array_product($res));
} }
while (array_product($res) !== $what) {
$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)) {
$res = self::native((int) $what); return $res;
if (is_array($res)) return $res; }
$res = self::native((int) $what);
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) {
$res = self::native_single((int) $what); return $res;
if ($res !== false) return $res; }
$res = self::native_single((int) $what);
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) { function random_string($length)
if (function_exists('random_bytes')) return random_bytes($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');
@ -93,7 +101,7 @@ for ($x = 0; $x < $tot; $x++) {
fclose($tg); fclose($tg);
foreach ($medium as $type => $total) { foreach ($medium as $type => $total) {
echo $type.': total time '.$total.', medium time '.($total/$tot).PHP_EOL; echo $type.': total time '.$total.', medium time '.($total / $tot).PHP_EOL;
} }
echo PHP_EOL.'------------------- SMALL MULTIPLE FACTOR TESTS -------------------'.PHP_EOL; echo PHP_EOL.'------------------- SMALL MULTIPLE FACTOR TESTS -------------------'.PHP_EOL;
$GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0]; $GLOBALS['medium'] = ['python' => 0, 'python_alt' => 0, 'wolfram' => 0, 'native' => 0];
@ -103,10 +111,9 @@ foreach ([200, 327, 35, 13589] as $multiple) {
} }
foreach ($medium as $type => $total) { 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];
@ -114,5 +121,5 @@ foreach ($m as $messy) {
test_single($messy, true); test_single($messy, true);
} }
foreach ($medium as $type => $total) { foreach ($medium as $type => $total) {
echo $type.': total time '.$total.', medium time '.($total/count($m)).PHP_EOL; echo $type.': total time '.$total.', medium time '.($total / count($m)).PHP_EOL;
} }