Home > OS >  How to calculate how many layers i can make from the available blocks to build a pyramide?
How to calculate how many layers i can make from the available blocks to build a pyramide?

Time:10-30

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?

CodePudding user response:

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

CodePudding user response:

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;
  • Related