项目欧拉:413

时间:2013-02-06 13:35:52

标签: php math

我正在尝试使用蛮力来解决最新的Project Euler问题(我知道这不是最好的解决方案,但是直到我弄清楚,我错了,我只会使用它。)

我无法理解,我到底做错了什么。我很确定我生成了正确的数字,但结果仍然是提供的数字错误:

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;

我得到378应该有389.我在这里做错了什么?

我不想要一个答案,只是关于我的逻辑错误的地方。


问题:

enter image description here

1 个答案:

答案 0 :(得分:3)

您只查看3位数字,但需要查看10 ^ 3以下的所有数字,包括1位和2位数字。换句话说,$start应为1

此外,您不会查看整个原始字符串的子字符串,因为$i(子字符串的长度)最多只能运行$length - 1