欧拉计画:413 [英] Project Euler: 413
问题描述
我正在尝试使用蛮力解决最新的欧拉计划问题(我知道这不是最好的解决方案,但是直到我弄清楚我弄错了什么地方,我才使用它).
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;
我得到378,应该有389.我在这里做错什么了?
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.
问题:
推荐答案
您只查看3位数字,但需要查看10 ^ 3以下的所有数字,包括1位和2位数字.换句话说,$start
应该是1
.
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, $start
should be 1
.
此外,您不会查看整个原始字符串的子字符串,因为$i
(子字符串的长度)只能运行到$length - 1
.
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
.
这篇关于欧拉计画:413的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!