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 Answers6

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.

Striezel
  • 3,693
  • 7
  • 23
  • 37
  • You shouldn't close ` – Zac Crites Sep 22 '16 at 16:49
  • Also your arguments to `implode` are in the wrong order. – Zac Crites Sep 22 '16 at 16:52
  • @ZacCrites: Well, it does not matter for that example, and on the other hand it feels a little bit weird to open a ` – Striezel Sep 22 '16 at 16:53
  • As for the arguments: According to the [PHP documentation](http://php.net/manual/en/function.implode.php) 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. – Striezel Sep 22 '16 at 16:57
  • http://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag – Zac Crites Sep 22 '16 at 16:58
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
Sherif
  • 11,786
  • 3
  • 32
  • 57
  • Great explanation! Thanks – Michiel Nuyts Sep 22 '16 at 17:16
  • 1
    @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. – Sherif Sep 22 '16 at 17:23
1

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

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

echo implode(" + ",$intArray)." = ".array_sum($intArray);
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
    }
}
nerdlyist
  • 2,842
  • 2
  • 20
  • 32
-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;
}
Zac Crites
  • 822
  • 10
  • 14
-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
Dennis
  • 7,907
  • 11
  • 65
  • 115