Skip to content

Instantly share code, notes, and snippets.

@tcopestake
Created July 17, 2017 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcopestake/ed9051a4a0e64236cfb5dd6a57f2a8a5 to your computer and use it in GitHub Desktop.
Save tcopestake/ed9051a4a0e64236cfb5dd6a57f2a8a5 to your computer and use it in GitHub Desktop.
I'm going to Hell for this
<?php
$position = 10001;
$primes = [ 2 => 2 ];
$miscreants = [ 1 => 1 ];
$x = $n = 2;
$target = $frame = ($position * 5);
$base = 0;
$g = true;
while (true) {
if ($x <= $target) {
$right = $x + 1;
if (!isset($miscreants[$right]) && $right <= $target) {
$primes[$right] = $right;
}
if (!$g) {
if (isset($primes[$x])) {
unset($primes[$x]);
}
} else {
}
$miscreants[$x] = $x;
}
if ($x >= $target) {
++$n;
if ($n >= $target) {
if (count($primes) >= $position) {
break;
}
$base = $target;
$n = 2;
$target += $frame;
} else {
}
$g = ($n > $base);
$m = $base ? ($base - ($base % $n)) : 0;
$x = $m + $n;
} else {
$x += $n;
$g = false;
}
}
$prime = array_slice($primes, $position - 1, 1);
echo $prime[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment