Home >Backend Development >PHP Tutorial >PHP implements Floyd's triangle

PHP implements Floyd's triangle

藏色散人
藏色散人Original
2019-03-22 14:44:493630browse

This article introduces how to use PHP to generate and display the first n rows of Floyd's triangle. (Use lines n=5 and n=11)

Freud's triangle is a set of right-angled triangular natural numbers used in computer science education. It is named after Robert Floyd. It is defined as filling the rows of a triangle with consecutive numbers, starting with 1 in the upper left corner:

PHP implements Floyd's triangle

PHP code example is as follows:

<?php
$n = 5; 
echo "n = " . $n . "\n";
$count = 1;
for ($i = $n; $i > 0; $i--) 
{
  for ($j = $i; $j < $n + 1; $j++) 
   {
     printf("%4s", $count);
     $count++;
   } 
	echo "\n";
   }

Output:

PHP implements Floyds triangle

Related recommendations: "PHP Tutorial"

This article is about An introduction to the method of implementing Floyd's triangle in PHP. It is simple and interesting. I hope it will be helpful to friends in need!

The above is the detailed content of PHP implements Floyd's triangle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn