doupinyan0186 2009-02-03 09:29
浏览 20
已采纳

项目Euler#5 php怀疑!

problem euler #5

i found the solution but i don't know why this first code is faster (i put 14 in order to try to make more clear the code) the only difference is that i eliminate the for i wrote for a huge if

if($num%14==0 && $num%13==0 &&$num%12==0 &&$num%11==0 &&$num%10==0 && $num%9==0 && $num%8==0 && $num%7==0 && $num%6==0 && $num%5==0 && $num%4==0 && $num%3==0 && $num%2==0 && $num%1==0){
    $notFound=0;
}

why is this second code hugely more slow than the first one? With the for it suppose to be faster. is the same in another languages???

$notFound=0;
for ( $i=14; $i>=2 && notFound==0; $i--){
    if($num%$i!=0){
        $notFound=1;
    }
}
  • 写回答

4条回答 默认 最新

  • duanquannan0593 2009-02-03 09:42
    关注

    The second code sample is simply performing more operations than the first, so I'd expect it to be slower. In this situation, you'd find that whilst offering poorer performance, a for loop offers significantly better readability and maintainability.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作