Skip to content

Instantly share code, notes, and snippets.

@bagart
Last active December 6, 2018 12:50
Show Gist options
  • Save bagart/83f6e72f5f4ca67dd60be6c4fb0d515a to your computer and use it in GitHub Desktop.
Save bagart/83f6e72f5f4ca67dd60be6c4fb0d515a to your computer and use it in GitHub Desktop.
big number has a has_factor
<?php
function chk_simple(int $value): bool {
$square_root_of_value = sqrt($value);
if (!($value % 2) ||!($value % 3) || $square_root_of_value === ceil($square_root_of_value)) {
return false;
}
$square_root_of_value = (int)$square_root_of_value;
$del3 = 0;
$iterations = 0;
for ($i = 5;$i < $square_root_of_value; $i = $i + 2) {
++$iterations;
if (++$del3 > 2) {
$del3 = 0;
//var_dump('skip:'. $del3 . '-'.$i);
continue;
}
if (!($value % $i)) {
var_dump([
'num' => $i,
'iterations' => $iterations
]);
return false;
}
}
var_dump([
'num' => false,
'iterations' => $iterations
]);
return true;
}
var_dump(chk_simple(126221));
var_dump(chk_simple(600851475143));
var_dump(chk_simple(2147483647));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment