4

So i have a simple for loop to get this result from any given number (get).
1 + 2 + 3 + 4 = 10

$num = intval($_GET["number"]);
$total = 0;

for ($i = 1; $i <= $num; $i++) {

    echo $i;

    if ($i != $num) {
        echo " + ";
    } 
    $total += $i;
}
    echo " = " . $total;

Now I want to show the calculation of every step
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
And it should be done with an Array, but I can't seem to figure out the Algorithm. I think I'm overlooking something simple here.

6
  • 1
    Add the value $i to an array every iteration. Then you want to print the calculation with implode() and get the result with array_sum(). Commented Sep 22, 2016 at 16:38
  • @Rizier123 I'll try that ! Thanks Commented Sep 22, 2016 at 16:39
  • @Sherif Oops that's a typo! Commented Sep 22, 2016 at 16:40
  • 1
    You may be interested in the range() function. Doing $nums = range(1, $num); will give you an array of ints. Commented Sep 22, 2016 at 16:43
  • 1 + 2 + 3 + 4 = (4 x (4 +1)) / 2 Commented Sep 22, 2016 at 16:43

6 Answers 6

1

Try something like this:

<?php
$num = intval($_GET["number"]);

//add all numbers to an array
$numbers = array();
for ($i = 1; $i <= $num; $i++)
{
  $numbers[] = $i;
  //show each array element with ' + ' in between the elements
  echo implode(' + ', $numbers);

  //show total sum
  echo " = " . array_sum($numbers) . "\n";
}
?>

Note that this does not work, if $_GET['number'] is zero or even below zero.

Sign up to request clarification or add additional context in comments.

5 Comments

You shouldn't close <?php tags since it can can cause whitespace to be accidentally written to the output buffer.
Also your arguments to implode are in the wrong order.
@ZacCrites: Well, it does not matter for that example, and on the other hand it feels a little bit weird to open a <?php tag but never close it.
As for the arguments: According to the PHP documentation you are only half right. implode() accepts arguments in both orders, and that is why the script also worked as intended when I tested it. Anyway, I've changed the order to avoid confusion.
1

Here's the simplest way I can think...

$num = intval($_GET['number']);
$intArray = range(1,$num);

echo implode(" + ",$intArray)." = ".array_sum($intArray);

Comments

1

You don't actually need a loop to do an arithmetic progression. An arithmetic progression like this can be calculated in constant time with the formula n * (n[-1] + n[1]) / 2.

For example the progression of 4, where n1 = 1, n2 = 2, n3 = 3, and n4 = 4 is simply 4 * (4 + 1) / 2 == 10.

function progression($n) {
    return $n * ($n + 1) / 2;
}

echo progression(4); // 10

However, to show the result of the progression at any given step you simply limit the upper-bound of that progression (i.e. $n).

$n = 4;
for ($i = 1; $i <= $n; $i++) {
    $operands = implode('+', range(1, $i));
    echo $operands . " = " . progression($i), "\n";
}

output

1 = 1
1+2 = 3
1+2+3 = 6
1+2+3+4 = 10

Generalization

This works for any linear arithmetic progression, regardless of the upper/lower bound. So for example the progression of 5 through 8 is still 4 * (5 + 8) / 2 which gives you 26.

So you can modify this function to a more general solution for any linear arithmetic progression as such.

function progression($size, $start = 1) {
    return $size * ($start + ($size + $start - 1)) / 2;
}

$n = 4;
$start = 5;
for ($i = $start; $i <= $n + $start - 1; $i++) {
    $operands = implode('+', range($start, $i));
    echo $operands . " = " . progression($i - $start + 1, $start), "\n";
}

output

5 = 5
5+6 = 11
5+6+7 = 18
5+6+7+8 = 26

2 Comments

Great explanation! Thanks
@MichielNuyts Sure, no problem. If you found the answer helpful don't forget to upvote it or accept it so that others might find it more easily as well if they have the same/similar question.
0

So assuming you are doing a range from the $_GET['number'] number then you can do something like (see comments in code for further explanation):

//This will create an array from 1 to number inclusive
$nums = range(1, $_GET['number']);
//The nums that have been used
$used = array();
//Now loop over that array
foreach($nums as $num){
    $used[] = $num; //Add this number to used
    if(count($used) > 1){//Dont care about first loop
        echo  implode(' + ', $used); // put all elements together by + sign
        echo ' = ' . array_sum($used) . "<br>"; //Show total plus a break
    }
}

Comments

-1
<?php

$num = intval($_GET["number"]);
$terms = [1];

for ($i = 2; $i <= $num; $i++) {
    $terms[] = $i;
    $sum = array_sum($terms);
    echo implode(' + ', $terms) . ' = ' . $sum . PHP_EOL;
}

Comments

-1

Going for maximum use of PHP array related functions:

$num = intval($_GET["number"]);
$array = range(1, $num);

for ($i = 2; $i <= $num; $i ++)
{
    $slice = array_slice($array, 0, $i);
    $total = array_sum($slice);
    echo implode(" + ", $slice) . " = " . $total . PHP_EOL;
}

Alternative with array_push

$num = intval($_GET["number"]);
$array = array(1);

for ($value = 2; $value <= $num; $value ++)
{
    array_push($array, $value);
    echo implode(" + ", $array) . " = " . array_sum($array) . PHP_EOL;
}

Output

1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10

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.