1

I've been trying to solve Project Euler problem 1 and I feel I'm missing something fundamental. Would you please help me fix my bug?

I tried this first:

<?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;
?>

Output:

Array

So I tried to do it with array_push instead, like this:

<?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;
?>

The output is the same. What am I missing?

1
  • arrays in string context will simply be Array. Commented Sep 25, 2014 at 20:50

3 Answers 3

6

Try this way:

print_r($multiplesOfNumbers);
Sign up to request clarification or add additional context in comments.

1 Comment

Also: echo array_sum($multiplesOfNumbers);
2

echo will not print the array elements. use print_r instead

Comments

0

You have to loop through the array to get the values of the array (using a foreach) or use something like var_dump() or print_r() to echo the array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.