I'm a real beginner with PHP, just had my first lesson. We have to create a program that prints Armstrong numbers (up to 10000).
The output should look like this.
0 ist eine Armstrong-Zahl: 0^1 = 0
[…]
8 ist eine Armstrong-Zahl: 8^1 = 8
9 ist eine Armstrong-Zahl: 9^1 = 9
153 ist eine Armstrong-Zahl: 1^3 + 5^3 + 3^3 = 153
370 ist eine Armstrong-Zahl: 3^3 + 7^3 + 0^3 = 370
371 ist eine Armstrong-Zahl: 3^3 + 7^3 + 1^3 = 371
407 ist eine Armstrong-Zahl: 4^3 + 0^3 + 7^3 = 407
1634 ist eine Armstrong-Zahl: 1^4 + 6^4 + 3^4 + 4^4 = 1634
8208 ist eine Armstrong-Zahl: 8^4 + 2^4 + 0^4 + 8^4 = 8208
9474 ist eine Armstrong-Zahl: 9^4 + 4^4 + 7^4 + 4^4 = 9474
That's what I came up with so far. It was working before but now it shows this error:
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\UE\HUE01\bsp-b\index.php on line 14
Code:
<?php
/**
* Created by PhpStorm.
* User: lisap
* Date: 08.03.2018
* Time: 18:49
*/
$number = 0;
while ($number>=0) {
$digits = str_split($number);
$count = count($digits);
$result = array_sum(array_map('pow', $digits, array_fill(0, $count, $count)));
if (($number == $result)) {
echo $number, ' ist eine Armstrong Zahl: ';
if ($number < 10) {
echo $digits[0].'^1 = '.$number.'<br/>';
}
if (($number > 9) && ($number < 100)) {
echo $digits[0].'^1'.$digits[1].'^2 = '.$number.'<br/>';
}
if (($number > 99) && ($number < 1000)) {
echo $digits[0].'^1'.$digits[1].'^2'.$digits[2].'^3 = '.$number.'<br/>';
}
if (($number > 999) && ($number < 10000)) {
echo $digits[0].'^1'.$digits[1].'^2'.$digits[2].'^3'.$digits[3].'^4 = '.$number.'<br/>';
}
$number++;
}
}
?>
The output look something like this right now.
0 ist eine Armstrong Zahl: 0^1 = 0
1 ist eine Armstrong Zahl: 1^1 = 1
2 ist eine Armstrong Zahl: 2^1 = 2
3 ist eine Armstrong Zahl: 3^1 = 3
4 ist eine Armstrong Zahl: 4^1 = 4
5 ist eine Armstrong Zahl: 5^1 = 5
6 ist eine Armstrong Zahl: 6^1 = 6
7 ist eine Armstrong Zahl: 7^1 = 7
8 ist eine Armstrong Zahl: 8^1 = 8
9 ist eine Armstrong Zahl: 9^1 = 9
Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\UE\HUE01\bsp-b\index.php on line 14