phpWhat is the number of daffodils?
The so-called "daffodil number" refers to an n-digit number (n≥3), the sum of the nth power of the numbers in each digit is equal to itself.
PHP program for finding the number of daffodils, there are many ways to write it:
Method Example 1:
<?php header("content-type:text/html;charset=utf-8;"); //设置页面编码为 utf-8 //以下代码求解1000以内的水仙花数 echo '<p>1000以内的水仙花数: </p>'; for ( $i = 100; $i < 1000; ++ $i ) { $hundreds = floor( $i / 100); //分解出百位 $tens = floor( $i / 10 ) % 10; //分解出十位 $ones = floor( $i % 10 ); //分解出个位 if (bcpow($hundreds,'3')+bcpow($tens,'3')+bcpow($ones,'3') == $i) echo $i.""; } ?>
Output:
1000以内的水仙花数: 153 370 371 407
Method example two:
<?php for($q=1;$q<=9;$q++){ for($w=0;$w<=9;$w++){ for($e=0;$e<=9;$e++){ if($q*$q*$q + $w*$w*$w + $e*$e*$e == 100*$q + 10*$w + $e){ echo "$q$w$e"."<p>"; } } } }
Output:
153 370 371 407
Method example three:
<?php function cube( $n ) { return $n * $n * $n; } function is_narcissistic ( $n ) { $hundreds = floor( $n / 100); //分解出百位 $tens = floor( $n / 10 ) % 10; //分解出十位 $ones = floor( $n % 10 ); //分解出个位 return (bool)(cube($hundreds)+cube($tens)+cube($ones) == $n); } for ( $i = 100; $i < 1000; ++ $i ) { if ( is_narcissistic($i) ) echo $i."\n"; }
Method example four :
<?php //阿姆斯特朗数:一个k位数,它的每个位上的数字的k次幂之和等于它本身。(例如:1^3 + 5^3 + 3^3 = 153) class Armstrong { static function index(){ for ( $i = 100; $i < 100000; $i++ ) { echo self::is_armstrong($i) ? $i . '<br>' : ''; } } static function is_armstrong($num){ $s = 0; $k = strlen($num); $d = str_split($num); foreach ($d as $r) { $s += bcpow($r, $k); } return $num == $s; } } Armstrong::index();
Output:
153 370 371 407 1634 8208 9474 54748 92727 93084
Method example five:
<html> <head> <title></title> </head> <body> <?php function winter($num) { if($num<1000){ //定义个位 $ge=$num%10; //定义十位 $ten=(($num%100)-$ge) /10; //定义百位 /*floor取整,忽略小数点后面的所有数*/ $hundred=floor($num/100); $sum1=$ge*$ge*$ge+$ten*$ten*$ten+$hundred*$hundred*$hundred; if($sum1==$num){ return 1; } else{ return 0; } } else{ return -1; } } if(winter(371)==-1) { echo "大于1000的数"; }else{ if(winter(371)) { echo "Yes"; } else{ echo "No"; } } ?> </body> </html>
Output:
Yes
Related recommendations: " PHP Tutorial》
The above is the detailed content of What is the daffodil number in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function