<?php
function isPrimeNumber($num) {
$isPrime = 1;
for ($i = 2; $i <= ($num/2); $i++) {
if ($num % $i == 0){
$isPrime = 0;
break;
}
}
return ($isPrime==1 || $num==2)? 1 : 0;
}
function generateRightTriangle( $num ) {
for ($i = 1, $rows = $num, $count = 2; $i <= $rows; $i++) {
/* Print spaces in decreasing order of row */
for($j=$i; $j<$rows; $j++)
{
printf("%s"," ");
}
for ($j = 1; $j <= $i; $j++) {
while( !isPrimeNumber($count) ){
$count++;
}
if ($i == 1) {
printf(" %d",$count);
}
else
if ($i == 2) {
printf(" %-d",$count);
}
else
if ($i == 3) {
if ($count == 7) {
printf(" %-d",$count);
}
else
{
printf(" %-2d",$count);
}
}
else
{
printf(" %-d", $count);
}
$count++;
}
printf("\n");
}
return true;
}
if (!generateRightTriangle(5)) {
trigger_error("An error has occurred!");
}
- Output for 7.1.25 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- 2
3 5
7 11 13
17 19 23 29
31 37 41 43 47
preferences:
144 ms | 403 KiB | 183 Q