Expected output :
my current code only output the pattern but it can't have more comb:
$n = 5;
$n1 = 4;
for ($i = 0; $i <= $n; $i++) {
for ($j = 0; $j <= $n1; $j++) {
if (($i == 0 || $i == 5) && ($j > 0 and $j < 3)) {
if (($i == 0 && $j == 1) || ($i == $n && $j != 2)) {
printf(" ");
}
printf("$i");
} elseif (($j == 0 || $j == 3) && ($i != 0 and $i != 5)) {
if ($j == 0 && $i != 1 && $i != 4) {
printf("");
} elseif (($j == 3 && $i != 1 && $i != 4)) {
printf(" ");
} elseif ($j == 0) {
printf(" ");
}
printf("$i");
} else {
printf(" ");
}
}
echo "<be>";
`
this is the output the only problem is how do i multiply the comb.
Just for fun I wrote it in JS, but probably pretty straightforward to convert to PHP (String.repeat => str_repeat, [].push => array_push, [].slice => array_slice, etc)
const arr = [];
const output = (x=1, y=1) => {
for (let k = 0; k < 5; k++) {
arr.push([' '.repeat(5-k), k, ' '.repeat(k), k, ' '.repeat(5-k)].join(''))
}
for (let k = 5; k <= 10; k++) {
arr.push([' '.repeat(k-5), 10-k, ' '.repeat(10-k), 10-k, ' '.repeat(k-5)].join(''))
}
arr.forEach((row, index) => {
arr[index] = row + row.substring(1).repeat(x - 1);
});
const orig = [...arr.slice(1)]
for (let i = 1; i < y; i++) {
arr.push(...orig);
}
console.log(arr.join('\n'))
}
output(2,3)
The simplest way is to create an array, where 1 = '*' and 0 = ' '.
This is the algorithm in PHP 7 that I have done to create the pattern.
Note: I have made changes to optimize the code
<?php
$max = 6;
// Arg
$arg1 = 2;
$arg2 = 2;
// Change to pair size when is non
if ($max % 2 != 0) {
++$max;
}
// Matrix with zero values
$t = \array_fill(0, $max * $arg1 - $arg1 + 1, \array_fill(0, $max * $arg2 - $arg2 + 1, 0));
// Lower: start left position
$lower = $max / 2 - 1;
// Upper: start right postion
$upper = $lower + 1;
// Set pattern in matrix
for ($i = 0, $n = $max - 1; $i <= $n; $i++, $lower--, $upper++) {
for ($a1 = 0; $a1 < $arg1; $a1++) {
for ($a2 = 0; $a2 < $arg2; $a2++) {
$y = $i + $max * $a1 - $a1;
$p = $max * $a2 - $a2;
// Set pixel pattern
$t[$y][$lower + $p] = $t[$y][$upper + $p] = 1;
}
}
// Restart lower?
if ($lower == 0) {
$lower = $max;
}
// Restart upper?
if ($upper == $n) {
$upper = -1;
}
}
echo '<pre>', \PHP_EOL;
foreach ($t as $r) {
foreach ($r as $v) {
if ($v) {
echo '*';
} else {
echo ' ';
}
}
echo \PHP_EOL;
}
echo '</pre>';
?>
Output:
** **
* * * *
* * *
* * *
* * * *
** **
* * * *
* * *
* * *
* * * *
** **
Related
I would like to make a program which can calculate first 5000 primary numbers whos ends with 9 :
I tried this but it didn't work :
$div9 = [];
$x = 2;
while (count($div9) <= 5000) {
function findPrime($x)
{
for ($i = 2; $i < ($x / 2); $i++) {
$rest = $x % $i;
if ($rest == 0) {
break;
}
}
return $x;
}
$primeList[] = $x;
for ($j = 0; $j < count($primeList); $j++) {
$array = array_map('intval', str_split($primeList[$j]));
if (end($array[$j]) === 9) {
return $primeList[$j];
$div9[] = $primeList[$j];
}
}
$x++;
}
any hints please?
You should not define a function inside your while loop
This should help
function check_prime($num)
{
if ($num == 1)
return false;
for ($i = 2; $i <= $num/2; $i++)
{
if ($num % $i == 0)
return false;
}
return true;
}
$div9 = [];
$i = 0;
while(count($div9) < 5000) {
if($i%10 === 9 && check_prime($i)) {
$div9[] = $i;
}
$i++;
}
Another variation on the theme, the isPrime function was ported from Javascript cryptoJS library.
# Adapted from CryptoJS v3.1.2
function isPrime( $n=0 ){
$r=sqrt( $n );
for( $f=2; $f <= $r; $f++ ){
if( !( $n % $f ) )return false;
}
return true;
}
function isFactor($n,$f){
return $n % 10 == $f;
}
$limit=5000;
$primes=[];
$x=2;
$f=9;
while( count( $primes ) < $limit ){
if( isPrime( $x ) && isFactor( $x, $f ) )$primes[]=$x;
$x++;
}
printf('<pre>%s</pre>',print_r($primes,true));
This question already has answers here:
Working with large numbers in PHP
(8 answers)
Closed 2 years ago.
Hello I would like to display in my code a full number to 20 decimal without scientific notation.
I think it's a memory problem in php.
can you help me ? thanks
function get($l, $c)
{
$value = 0;
if (0 <= $c && $c <= $l && $l < 5000) {
$tab = [];
for ($i = 0; $i <= $l; $i++) {
for ($j = 0; $j <= $c; $j++) {
if ($i == $j || $i - 1 <= 0 || $j <= 0) {
$tab[$i][$j] = 1;
$value = $tab[$i][$j];
} elseif ($i != $j) {
$tab[$i][$j] = ($tab[$i - 1][$j - 1]) + ($tab[$i - 1][$j]);
$value = $tab[$i][$j];
}
}
}
}
return $value;
}
echo get(67, 34); // found :1.422652073762E+19 , excpected:14226520737620288370
Have a look at BCMath functions:
function get($l, $c)
{
$value = 0;
if (0 <= $c && $c <= $l && $l < 5000) {
$tab = [];
for ($i = 0; $i <= $l; $i++) {
for ($j = 0; $j <= $c; $j++) {
if ($i == $j || $i - 1 <= 0 || $j <= 0) {
$tab[$i][$j] = 1;
$value = $tab[$i][$j];
} elseif ($i != $j) {
// magic happens here
$tab[$i][$j] = bcadd($tab[$i - 1][$j - 1], $tab[$i - 1][$j]);
$value = $tab[$i][$j];
}
}
}
}
return $value;
}
$result = get(67, 34);
var_dump($result == '14226520737620288370');
echo $result;
Output
bool(true)
14226520737620288370
Working example.
There is that : https://www.php.net/manual/en/function.number-format.php
You can print any number and choose number of decimals you want
I have this gauss_eliminate function, but instead of dealing with real numbers, I want it to work on binary values.
I need the GF2 gauss_eliminate function, where the input is binary and the output is binary.
This produces real values, not binary, eg
0.57142857142857
0.71428571428571
-0.42857142857143
-0.28571428571429
0.14285714285714
Gaussian elimination has these 3 allowed steps:
1) Swapping two rows (for achieving a certain look)
2) Multiplying a row by a nonzero number,
3) Adding a multiple of one row to another row.
-- in GF2: addition operation is XOR : 0+0=0, 0+1=1, 1+0=1, 1+1=0 --and--
multiplication is AND operation: 0*0=0,0*1=0,1*0=0,1*1=1
function gauss_eliminate($A, $b, $N)
{
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) {
$j = $i;
$max = $tmp;
}
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
$tmp = $A[$i][$col] / $A[$col][$col];
for ($j = $col + 1; $j < $N; $j++) $A[$i][$j] -= $tmp * $A[$col][$j];
$A[$i][$col] = 0;
$b[$i] -= $tmp * $b[$col];
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
$tmp = $b[$col];
for ($j = $N - 1; $j > $col; $j--) $tmp -= $x[$j] * $A[$col][$j];
$x[$col] = $tmp / $A[$col][$col];
}
return $x;
}
new code #1, still doesn't work:
function gauss_eliminate($A, $b, $N)
{
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) {
$j = $i;
$max = $tmp;
}
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
for ($j = $col + 1; $j < $N; $j++)
$A[$i][$j]=( $A[$i][$j] != $A[$col][$j] ) ? 1 : 0;
$A[$i][$col] = 0;
$b[$i]=( $b[$i] != $b[$col] ) ? 1 : 0;
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
# $tmp = $b[$col];
# for ($j = $N - 1; $j > $col; $j--) $tmp -= $x[$j] * $A[$col][$j];
$x[$col] = ( $x[$col] != $A[$col][$j] ) ? 1 : 0;
}
return $x;
}
New code #2 - still doesn't work - tmp setup to alternate
function gauss_eliminate($A, $b, $N)
{
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) { $j = $i; $max = $tmp; }
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
# $tmp = $A[$i][$col] / $A[$col][$col];
for ($j = $col + 1; $j < $N; $j++) $A[$i][$j]=($A[$i][$j] != $A[$col][$j] ? 1 : 0);
$A[$i][$col] = 0;
$b[$i] = ( $b[$i] != $b[$col] ? 1 : 0);
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
$tmp = $b[$col];
for ($j = $N - 1; $j > $col; $j--) $tmp = 1 - $tmp;
$x[$col] = ($tmp != $A[$col][$j] ? 1 : 0);
}
return $x;
}
It appears I found the right syntax. I am getting the right result for one example, after modifying the code in a way that makes sense.... converting - to +, and this + to XOR, while / is ignored and * is AND.
Still it would be nice to get a confirmation that this code is correct.
function gauss_eliminate($A, $b, $N) {
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) {
$j = $i;
$max = $tmp;
}
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
# $tmp = $A[$i][$col] / $A[$col][$col];
# for ($j = $col + 1; $j < $N; $j++) {
# $A[$i][$j] -= $tmp * $A[$col][$j];
# }
# $A[$i][$col] = 0;
# $b[$i] -= $tmp * $b[$col];
$tmp = $A[$i][$col];
for ($j = $col + 1; $j < $N; $j++) {
# $A[$i][$j] = $A[$i][$j] + ( $tmp * $A[$col][$j] );
$A[$i][$j] = ( $A[$i][$j] != ( $tmp && $A[$col][$j] ) ) ? 1 : 0;
}
$A[$i][$col] = 0;
# $b[$i] = $b[$i] + ($tmp * $b[$col]);
$b[$i] = ( $b[$i] != ($tmp && $b[$col]) ) ? 1 : 0;
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
$tmp = $b[$col];
for ($j = $N - 1; $j > $col; $j--) {
# $tmp -= $x[$j] * $A[$col][$j];
# $tmp = $tmp + ($x[$j] * $A[$col][$j]);
$tmp = ( $tmp != ($x[$j] && $A[$col][$j]) ) ? 1 : 0;
}
# $x[$col] = $tmp / $A[$col][$col];
$x[$col] = $tmp;
}
return $x;
}
The commented text is the old (non GF2 code) as well as my "middle step" of showing where I convert + to XOR, * to AND, etc
I was trying to make function that gives two value. First value was the respite in the radical and the second value is the number that we want to put it in radical.
It's my code:
function radical($respite = 2, $num)
{
$numbers = array();
for ($i = 1; $i < 10; $i++) {
$numbers[] = '0.' . "$i";
}
for ($i = 1; $i < 10; $i++) {
$numbers[] = '1.' . "$i";
}
// I wanted do these loop until to creat numbers from 0.1 to 100,
// but i under stand it's silly work and wrong.
for ($i = 0; $i < sizeof($numbers); $i++) {
if ($respite == 2) {
$hesan = $number["$i"] * $number["$i"];
if ($hesab == $num) {
return $hesab;
}
} elseif ($respite == 3) {
$hesan = $number["$i"] * $number["$i"] * $number["$i"];
if ($hesab == $num) {
return $hesab;
}
}
}
}
I tried to create numbers from 0.1 to 100. and I wanted write if $number[$i] * $number[$i] = $num return the $number, but I saw it's silly work and wrong my means create numbers from 0.1 to 100 by this way.
For radical(2, 9) the output should be 4, because 3 * 3 = 9.
If the first value is 3 radical(3, 8) the output should be 2, because 2 * 2 * 2= 8
Can someone make function to do radical with respite ? or improve my code ?
If you want to do this not as an exercise but for productive use, I suggest this:
function radical($num, $respite = 2)
{
return $num ** (1 / $respite);
}
echo radical(27, 3) . "\n" .
radical(8, 3) . "\n" .
radical(36, 2) . "\n" .
radical(16, 2);
Output:
3
2
6
4
You can see it here
The reason I changed the argument order is that you can't have a required parameter after an optional one.
Hope this is what you are looking for..
Try this code snippet here
function radical($respite = 2, $num)
{
for($x=0;$x<=10;$x+=0.1)
{
$numbers[]=$x;
}
for ($i = 0; $i < sizeof($numbers); $i++)
{
if ($respite == 2)
{
if ((string)($numbers[$i] * $numbers[$i]) == (string)$num)
{
return $numbers[$i];;
}
}
elseif ($respite == 3)
{
if ((string)($numbers[$i] * $numbers[$i]* $numbers[$i]) == (string)$num)
{
return $numbers[$i];
}
}
}
}
print_r(radical(3, 27));//3
print_r(radical(3, 8));//2
print_r(radical(2, 36));//6
print_r(radical(2, 16));//4
I'm not sure what you're going for exactly, but I think this cleans up what you have now at least:
function radical($num, $respite = 2) {
$numbers = array();
for ($i = 1; $i <= 1000; $i++) {
$numbers[] = $i * 0.1;
}
foreach($numbers as $i) {
if ($respite == 2) {
$hesab = $i * $i;
if ($hesab == $num) {
return $i;
}
}
elseif($respite == 3) {
$hesab = $i * $i * $i;
if ($hesab == $num) {
return $i;
}
}
}
return 0;
}
I need to create a pyramid using asterisks. I specify a value which becomes the base of the pyramid. The base contains as much asterisks as the value specified and the pyramid must skip its rows by 1..Here I am facing a problem when I specify an even number of base..
The pyramid must looke like the one below.
*
***
*****
*******
*********
**********
I am getting
####*
###***
##*****
###*****
####*****
**********
I want to replace the # by some blank space and I am getting the bug that the number of asterisks in the 4th row has decreased.. How do I fix these two bugs ?
function create_pyramid($limit){
if ($limit > 0){
for ($row =0;$row<=$limit;$row++){
if (($row % 2 == 0) && ($row != $limit)){ continue;}
$rows = "";
for ($col =0;$col<$row;$col++){
$rows= $rows.'*';
}
$pattern = "%'#".((($limit - $row)/2)+$row)."s\n";
printf ($pattern,$rows);
print '<br />';
}
}
else{
print "Invalid data";
}
}
create_pyramid(10);
I prefer mine :
echo '<pre>';
$n = 5;
function print_tree($n, $str, $max) {
for ($i = 0; ($i < (($max - $n) / 2)); $i++) {
echo " ";
}
for ($i = 0; ($i < $n); $i++) {
echo $str;
}
echo "<br/>";
}
for ($flag = 0; ($flag < 2); $flag++) {
for ($a = 1, $b = 1, $c = 1, $d = 4; (($d - 3) <= $n); $a += 2, $b++) {
if ($flag == 1) {
print_tree($a, "*", $max);
}
if ($b == $d) {
if ($flag == 0) {
$max = $a;
}
if (($d - 3) != $n) {
$a -= ((2 * $c) + 2);
}
$b = 0;
$d++;
if (($d % 2) == 0) {
$c++;
}
}
}
}
if ((($foot = $n) % 2) == 0) {
$foot++;
}
for ($i = 0; ($i < $foot); $i++) {
print_tree($foot, "|", $max);
}
outputs :
*
***
*****
*******
*****
*******
*********
***********
*************
***********
*************
***************
*****************
*******************
*********************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
*************************
***************************
*****************************
*******************************
*********************************
***********************************
*************************************
***************************************
|||||
|||||
|||||
|||||
|||||
Or even this one:
<?php
$n = 8;
ob_start();
$stars = ($n - 1) * 2 + 1;
$spaces = 0;
for ($i = 0; ($i < $n); $i++) {
echo str_repeat(' ', $spaces);
echo str_repeat('*', $stars);
echo ' ';
echo str_repeat(' ', $spaces * 2);
echo str_repeat('*', $stars);
echo "\n";
$spaces += 1;
$stars -= 2;
}
$stars = ($n - 1) * 2 + 1;
$spaces = 0;
$margin = $stars / 2 + 1;
for ($i = 0; ($i < $n); $i++) {
echo str_repeat(' ', $margin);
echo str_repeat(' ', $spaces);
echo str_repeat('*', $stars);
echo "\n";
$spaces += 1;
$stars -= 2;
}
echo trim(implode("\n", array_reverse(explode("\n", ob_get_clean()))), "\n"), "\n";
it gives:
*
***
*****
*******
*********
***********
*************
***************
* *
*** ***
***** *****
******* *******
********* *********
*********** ***********
************* *************
*************** ***************
funny exercices isn't it... 8-)
You can try
create_pyramid("*", 5);
create_pyramid("#", 10);
create_pyramid("^_^", 10);
function create_pyramid($string, $level) {
echo "<pre>";
$level = $level * 2;
for($i = 1; $i <= $level; $i ++) {
if (!($i % 2) && $i != 1)
continue;
print str_pad(str_repeat($string, $i),($level - 1) * strlen($string), " " , STR_PAD_BOTH);
print PHP_EOL;
}
}
Output A
*
***
*****
*******
*********
Output B
#
###
#####
#######
#########
###########
#############
###############
#################
###################
Output C
^_^^_^^_^
^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
Just make it simpler:
function create_pyramid($limit) {
for($row = 1; $row < $limit; $row ++) {
$stars = str_repeat('*', ($row - 1) * 2 + 1);
$space = str_repeat(' ', $limit - $row);
echo $space . $stars . '<br/>';
}
}
echo "<pre>" ;
create_pyramid(10);
<?php
$n=9;
for($i=0; $i<=$n; $i++)
{
for($j=1; $j<=$i; $j++)
echo " ";
for($k=1; $k<=$n-$i; $k++)
echo $k;
for($j=($k-2); $j>0; $j--)
echo $j;
for($k=1; $k<=$i; $k++)
echo " ";
echo "</br>";
}
?>
function create_row($num, $limit) {
$append = '';
if($limit > $num && ($limit - $i) % 2 == 0) {
$append .= '-';
}
$stars = str_repeat('*', $num);
$ap_len = floor(($limit - $num) / 2);
$prepend = str_repeat('-', $ap_len);
$append .= str_repeat('-', $ap_len);
return $prepend . $stars . $append;
}
function create_pyramid($limit){
if ($limit > 0){
$no_last = false;
for($i = 1; $i <= $limit; $i += 2) {
print create_row($i, $limit) . PHP_EOL;
if($i == $limit) {
$no_last = true;
}
}
if(!$no_last) {
print create_row($limit, $limit) . PHP_EOL;
}
}
}
create_pyramid(10);
From what I understand, what you are looking for is an Odd numbered pyramid, i.e. the number of * in each row is as per odd number series, like 1,3,5,7. If you wish to include "if-else" statements then you can go with the above answered loops, but if you only wish to use "for" loops, then you can use the following code:
<?php
$x=1;
for($i=1;$i<7;$i++)
{
for($j=7;$j>$i;$j--)
{
echo ' ';
}
for($k=1;$k<=$x;$k++)
{
echo '*';
}
$x=$x+2;
echo "<br/>";
}
?>
I think that the simplest solution is to create 2 loops with condition:
$n = 5; // specify how many rows you want to
$stars = 0;
for ($i = $n; $i > 0; $i--) {
for ($j = 0; $j < $i + $stars; $j++) {
if ($j < $i - 1) {
echo " ";
} else {
echo "*";
}
}
$stars += 2;
echo "\n";
}
The output will be:
*
***
*****
*******
*********
<?php
$l=5;
for ($i=1; $i <=5; $i++) {
for ($k=1; $k < $l ; $k++) {
echo ' ';
}
for ($j=0; $j<$i; $j++) {
echo '*';
}
$l--;
echo "<br>";
}
?>
function create_piramide ($filas) {
$n = $filas;
echo '<pre>';
for ($i = $filas; $i >= 0; $i--) {
echo str_repeat(' ', $i).str_repeat('o ', $n - $i)."\n";
}
echo '</pre>';
}
create_piramide (10);
function pyramid($height)
{
for ($i = 1; $i <= $height; $i++) {
echo str_repeat(" ", $height - $i);
echo str_repeat('*', $i * 2 - 1) . '<br/>';
}
}
pyramid(5);
Diamond Shape
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
<?php
$num=15;
$num2=0;
$half = $num;
for($i=$num2; $i<=$num; $i++)
{
for($j=$half; $j>$i; $j--)
{
echo " ";
}
for($j=$num2; $j<=$i; $j++)
{
echo " * ";
}
echo "<br>";
}
for($i=$num2; $i<=$num; $i++)
{
for($j=$num2; $j<=$i; $j++)
{
echo " ";
}
for($j=$half; $j>$i; $j--)
{
echo " * ";
}
echo "<br> ";
}
?>
$n = 5;
for($i = $n; $i >= 1; $i--){
for($j = 1; $j <= $i; $j++){
if($j < $i){
echo '0';//you can replace 0 with space
}else{
$num = $n - $i;
for($k = 0; $k <= $num; $k++){
echo '*';
}
for($y = 1; $y <= $n - 1; $y++){
if($y <= $num){
echo '*';
}else{
echo '0';//you can replace 0 with space
if($y == $n - 1){
echo '<br/>';
}
}
}
}
}
}
$n1=10;
$k=$n1/2;
$n=$n1/2;
for($i=0 ; $i<=$n1 ; $i++) {
if($i <= $n1/2) {
$k=$k+1;
$n=$n-1;
} else {
$k=$k-1;
$n=$n+1;
}
for($j=0 ; $j<=$n1 ; $j++) {
if($j < $k && $j > $n) {
echo "*";
} else {
echo "  ";
}
}
echo "<br/>";
}
<?php
ini_set('display_errors', 1);
/**
* Here is my solution for printing the pyramid using a class
*/
class pyramid
{
function __construct($rows)
{
for ($k=1; $k <= $rows ; $k++) {
$this->space($rows,$k);
$this->left($k);
$this->right($k);
echo "<br>";
}
}
public function left($k)
{
for ($i=1; $i < $k ; $i++) {
echo $i;
}
}
public function right($i)
{
for ($j=$i; $j >= 1 ; $j--) {
echo $j;
}
}
public function space($rows,$k)
{
for ($i=$rows-$k; $i > 0 ; $i--) {
echo " ";
}
}
}
$pyramid = new pyramid(5);
?>
<style type="text/css">
body{
font-family: monospace;
font-size: 24px;
}
</style>