skip to Main Content

I’m still a beginner in php and need some help from you with my coding. I need to calculate how many layers i can make with the available blocks.

see example below. For example, if i have 6 blocks, the pyramide will be 3 layers high, with 10 blocks 4 layers high ect. See example below

pyramid

Thank you in advance.

<?php

$blocks = readline("Enter how many blocks are available for a pyramid?") . PHP_EOL;
$h = 1;

while ($h <= $blocks) {
    $layers = $h++;
}

echo $layers;

Is there any solution for this?

2

Answers


  1. This might be an approach:

    <?php
    $numberOfBlocks = readline("Enter how many blocks are available for a pyramid? ") . PHP_EOL;
    
    $numberOfLayers = 0;
    do {
      $numberOfBlocks -= ($numberOfLayers + 1);
      if ($numberOfBlocks < 0) 
        break;
      $numberOfLayers++;
    } while (true);
    
    echo "Number of possible layers: $numberOfLayers" . PHP_EOL;
    

    The strategy: you decrement the number of blocks required for the next lower layer in each iteration. If the resulting number of blocks is smaller than zero (there were not enough blocks for that layer), then skip and output the layers so far. Otherwise count that full layer and start over again.


    @NigelRen pointed out another approach in his comment below. I had to adjust it slightly, but it leads to a much more compact solution:

    <?php
    $numberOfBlocks = readline("Enter how many blocks are available for a pyramid? ") . PHP_EOL;
    
    for ($numberOfLayers = 0; $numberOfBlocks >= $numberOfLayers + 1; $numberOfBlocks -= ++$numberOfLayers);
    
    echo "Number of possible layers: $numberOfLayers" . PHP_EOL;
    

    That would be a corresponding unit test class:

    class unittest extends TestCase
    {
        /**
         * @dataProvider expectedNumberOfLayers
         */
        public function testAdd($numberOfBlocks, $expectedNumberOfLayers) {
            $this->assertEquals($expectedNumberOfLayers, computeLayersFromBlocks($numberOfBlocks));
        }
        
        public function expectedNumberOfLayers(): Array {
            return [
                [ 0, 0],
                [ 1, 1],
                [ 2, 1],
                [ 3, 2],
                [ 4, 2],
                [ 5, 2],
                [ 6, 3],
                [ 7, 3],
                [ 8, 3],
                [ 9, 3],
                [10, 4],
                [11, 4],
                [12, 4],
                [13, 4],
                [14, 4],
                [15, 5],
                [16, 5],
                [17, 5],
                [18, 5],
                [19, 5],
                [20, 5],
                [21, 6],
                [22, 6],
                [23, 6],
                [24, 6]
            ];
        }
    }
    
    Login or Signup to reply.
  2. So, the no. of blocks per layer increases consecutively like,

    1
    2
    3
    4
    5
    ... etc
    

    You have been given the no. of blocks and you wish to find out the layers. This is like the sum of n consecutive numbers where n is no. of the layers and sum is the total blocks available.

    The math formula is ,

    sum = n * (n + 1) / 2;
    

    In our case, we are given the sum and we need to find the value of n that is a nearest match to the sum when substituted in the above formula. We can simplify the equation as:

    sum = n * (n + 1) / 2;
    sum * 2 = n * (n + 1);
    

    So our code would look like,

    <?php
    
    $blocks = readline("Enter how many blocks are available for a pyramid?") . PHP_EOL;
    
    // sum * 2 = n * (n + 1);
    
    $blocks = intval($blocks) * 2;
    
    $ans = 0;
    
    for($layer = 1; ; ++$layer){
      if($layer * ($layer + 1) <= $blocks){
        $ans = $layer;
      }else{
        break;
      }
    }
    
    echo $ans;
    

    Online Demo


    For an efficient approach, you can rewrite the equation as:

    sum = n * (n + 1) / 2;
    sum * 2 = n * (n + 1);
    n * (n + 1) - sum * 2 = 0;
    n^2 + n - 2 * sum = 0;
    

    Using the quadratic formula, we can directly get the value of n as below,

    <?php
    
    $blocks = readline("Enter how many blocks are available for a pyramid?") . PHP_EOL;
    
    echo ((int)sqrt(1 + 4 * 2 * intval($blocks)) - 1) >> 1;
    

    Online Demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search