-
-
- php daffodil number function--bbs.it-home.org
- < ;body>
- function winter($num)
- {
- if($num<1000){
- //Define the ones digit
- $ge=$num%10;
- //Define the tens digit
- $ten =(($num%100)-$ge) /10;
- //Define the hundreds place
- /*floor rounding, ignore all numbers after the decimal point*/
- $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 "greater than The number of 1000";
- }else{
- if(winter(371)) {
- echo "Yes";
- } else{
- echo "No";
- }
- }
- ?>
- < ;/html>
-
Copy code
Example 2, PHP implements narcissus number
-
- for($i=0;$i<1000;$i++)
- {
- $a=floor($i/100);//Find the hundreds digit
- $b =floor($i/10)%10;//Find the tens digit
- $c=$i%10;//Find the ones digit
- //if($a*$a*$a+$b* $b*$b+$c*$c*$c==$i)
- if(pow($a,3)+pow($b,3)+pow($c,3)==$i)/ /Determine whether the sum of the cubes of hundreds, tens and units is equal to the number itself
- {
- echo $i."
";
- }
- }
- ?>
-
Copy code
|