欧拉计画:413

编程入门 行业动态 更新时间:2024-10-19 08:54:04
本文介绍了欧拉计画: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

本文发布于:2023-11-29 20:44:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647580.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计画   欧拉

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!