Skip to content

Instantly share code, notes, and snippets.

@trys
Created April 8, 2015 16:09
Show Gist options
  • Save trys/f82bb01f97fa07e4502a to your computer and use it in GitHub Desktop.
Save trys/f82bb01f97fa07e4502a to your computer and use it in GitHub Desktop.
Problem Four
for ( $i = 998001; $i >= 10000; $i-- ) {
// Cast as string and calculate length;
$test = (string) $i;
$length = strlen( $test );
// Check if we have a the first part of a palindromical number
if ( ! ( $test[0] === $test[ $length - 1 ] && $test[1] === $test[ $length - 2 ] ) )
continue;
// If it's a 6 digit number and palindromical or if it is 5
if ( ! ( $length === 6 && $test[2] === $test[ $length - 3 ] || $length === 5 ) )
continue;
// Check if the number has a factor
if ( has_factor( $test ) ) {
echo $test;
break;
}
}
function has_factor( $product ) {
for ( $d = 100; $d <= 999; $d++ ) {
if ( $product % $d !== 0 )
continue;
$divisor = $product / $d;
if ( $divisor >= 100 && $divisor <= 999 )
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment