I’m trying to solve the latest Project Euler problem using brute force ( I know it’s not the best solution, but till I figure out, where I’m mistaken, I’ll just use that ).
I can’t understand, what exactly am I doing wrong here. I’m pretty sure that I generate the correct numbers, but the outcome is still wrong with the provided numbers:
function number_split( $nb )
{
GLOBAL $arr;
$length = strlen( $nb );
if( $length == 1 )
return true;
$count = 0;
for( $i=1; $i<$length; $i++ ) {
for( $j=0; $j<=$length-$i; $j++ ) {
$temp = substr( $nb, $j, $i );
if( $temp % $length == 0 ) {
$count++;
if( $count > 1 )
return false;
}
}
}
return ( $count == 1 );
}
$lim = 3;
$res = 0;
$start = gmp_strval( gmp_pow( 10, $lim-1 ) );
$end = gmp_strval( gmp_pow( 10, $lim ) );
for( $i=$start; $i<$end; $i++ ) {
$res += number_split( $i );
}
echo $res;
I get 378 where there should be 389. What am I doing wrong here?
I don’t want an answer, just a hitch on where my logic is wrong.
The problem:

You are only looking at 3-digit numbers, but you need to look at all numbers under 10^3, which includes 1-digit and 2-digit numbers. In other words,
$startshould be1.Additionally, you aren’t looking at the substring which is the entire original string, because
$i(the length of the substring) only runs up to$length - 1.