Simple PHP calculation using an array

So, I have a simple 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 each step
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
And this should be done using an array, but I can not understand the algorithm. I think I missed something simple here.

+4
source share
6 answers

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 it $_GET['number']is zero or even below zero.

+1
source

, . , , n * (n[-1] + n[1]) / 2.

, 4, n1 = 1, n2 = 2, n3 = 3 n4 = 4 - 4 * (4 + 1) / 2 == 10.

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

echo progression(4); // 10

, , (.. $n).

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

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

/ . , , 5 8 4 * (5 + 8) / 2, 26.

, .

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";
}

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

, ...

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

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

, $_GET['number'], - (. ):

//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
    }
}
0
source
<?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;
}
-1
source

Transition to the maximum use of functions connected with PHP:

$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
-1
source

Source: https://habr.com/ru/post/1655503/


All Articles