我一直在努力解决Project Euler问题1,我觉得我缺少一些基本的东西.你能帮我解决一下我的错误吗?
我先试了一下:
<?php
/*
** Project Euler Problem 1
** If we list all the natural numbers below 10 that are multiples of 3 or 5,
** we get 3, 5, 6 and 9. The sum of these multiples is 23.
** Find the sum of all the multiples of 3 or 5 below 1000.
*/
$numberOne = 3;
$numberTwo = 5;
$endingIndex = 1000;
$multiplesOfNumbers = array();
for ($index = 1; $index <= $endingIndex; $index++) {
if ($index % $numberOne == 0 && $index % $numberTwo == 0) {
$multipleOfNumbers[] = $index;
}
}
echo $multiplesOfNumbers;
?>
Run Code Online (Sandbox Code Playgroud)
输出:
排列
所以我尝试用它array_push代替,像这样:
<?php
/*
** Project Euler Problem 1
** If we list all the natural numbers below 10 that are multiples of 3 or 5,
** we get 3, 5, 6 and 9. The sum of these multiples is 23.
** Find the sum of all the multiples of 3 or 5 below 1000.
*/
$numberOne = 3;
$numberTwo = 5;
$endingIndex = 1000;
$multiplesOfNumbers = array();
for ($index = 1; $index <= $endingIndex; $index++) {
if ($index % $numberOne == 0 && $index % $numberTwo == 0) {
// $multipleOfNumbers[] = $index;
array_push($multiplesOfNumbers, $index);
}
}
echo $multiplesOfNumbers;
?>
Run Code Online (Sandbox Code Playgroud)
输出是一样的.我错过了什么?
小智 6
试试这种方式:
print_r($multiplesOfNumbers);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
47 次 |
| 最近记录: |