Skip to content

Instantly share code, notes, and snippets.

@uniacid
Created July 6, 2016 20:26
Show Gist options
  • Save uniacid/e889750eea4f641143b780faada2d14e to your computer and use it in GitHub Desktop.
Save uniacid/e889750eea4f641143b780faada2d14e to your computer and use it in GitHub Desktop.
Find the 444th Prime number converted to hexadecimal which contains the word "beef"
<?php
set_time_limit(0);
function isPrime($number) {
$sqrt = ceil(sqrt($number));
if ($number <= 1) {
return FALSE;
} else if ($number <= 3) {
return TRUE;
} else if ( !($number % 2) || !($number % 3) ) {
return FALSE;
}
for($i = 3; $i <= $sqrt; $i = $i+2) {
if (!($number % $i)) {
return FALSE;
}
}
return TRUE;
}
function findString($occurrence=1, $string='beef') {
$i = 1;
$current = 0;
while (TRUE) {
if (strpos(dechex($i), $string) !== FALSE && isPrime($i) === TRUE) {
$current++;
if ($current === $occurrence) {
return $i;
}
}
$i++;
}
}
//find first prime with beef
$beef = findString();
var_dump($beef, dechex($beef));
//find 444th prime with beef
$beef = findString(444);
var_dump($beef, dechex($beef));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment