Nudiowi Posted October 28, 2022 Share Posted October 28, 2022 Hey guys, 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. 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? Quote Link to comment https://forums.phpfreaks.com/topic/315467-how-to-calculate-how-many-layers-i-can-make-from-the-available-blocks-to-build-a-pyramide/ Share on other sites More sharing options...
Barand Posted October 28, 2022 Share Posted October 28, 2022 If a pyramid has L layers, you need blocks = L * (L + 1) / 2 Quote Link to comment https://forums.phpfreaks.com/topic/315467-how-to-calculate-how-many-layers-i-can-make-from-the-available-blocks-to-build-a-pyramide/#findComment-1601991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.