dongyuan1160 2009-11-17 20:49
浏览 56
已采纳

php谜语 - 有趣的结果

I have the following code:

<?php

$cups = array();
for($i=0; $i<500; $i++){
    $cups[$i] = 0;
}

for($x=1; $x<500; $x++){
    for($y=$x; $y<500; $y+=$x){
        $cups[$y] = !$cups[$y];
    }
}

foreach($cups as $key => $value){
    if($value == 1){
        echo "{$key}, ";
    }
}

?>

As you can see, I fill up an array with 500 zeroes, loop through it twice, and then print out the cup numbers that have a '1' in them:

1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484,

As you can see - it outputs squares. I think the phenomenon is impressive, but I am interested in a mathematical explanation -

Why does this pattern occur?

Thanks!

  • 写回答

3条回答 默认 最新

  • dongsui3297 2009-11-17 20:58
    关注

    It works this way because this is the classic Locker Problem... and in the locker problem, only the numbers with odd number of factors are returned... which are all the squares.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!