dsjj15012 2018-03-19 18:35
浏览 35

在PHP中的Armstrong Number程序中出现“超过30秒的最大执行时间”错误

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
  • 写回答

2条回答 默认 最新

  • dqellle310167 2018-03-19 18:55
    关注

    One of your problems is that you are using the function str_split in the wrong way. From the docs:

    If the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length.

    So, replace

    $digits = str_split($number,10000);
    

    With

    $digits = str_split($number);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计