Home  >  Article  >  Backend Development  >  4 examples of how to implement the number of daffodils in PHP_PHP tutorial

4 examples of how to implement the number of daffodils in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:34:331028browse

Example 1:

Copy code The code is as follows:

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 "."

";

Example 2:



Copy code

The code is as follows:

function cube( $n ){ return $n * $n * $n;}
function is_narcissistic ( $n )
{
$hundreds = floor( $n / 100); // Decompose Hundreds place
$tens = floor( $n / 10 ) % 10; // Decompose the tens place
$ones = floor( $n % 10 ); // Decompose the ones place
return (bool )(cube($hundreds)+cube($tens)+cube($ones) == $n);
}


for ( $i = 100; $i < 1000 ; ++ $i )
{
if ( is_narcissistic($i) )
echo $i."n";
}
?>



Example 3:



Copy code
The code is as follows:


//Armstrong number: A k-digit number, the sum of the k-th powers of the digits in each digit is equal to itself. (For example: 1^3 + 5^3 + 3^3 = 153) class Armstrong { static function index(){ for ( $i = 100; $i < 100000; $i++ ) {
echo self::is_armstrong($i) ? $i . '
' : '';
}
}
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();



Example 4:



Copy code

The code is as follows:




function winter($num)
{
if($num<1000){
//Define the ones place
$ge=$num%10;
//Define ten Digits
$ten=(($num%100)-$ge) /10;
//Define hundreds place
/*floor rounding, ignoring 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 "A number greater than 1000";
if(winter(371)) {

echo "Yes" ;

                                                                                                                                                                                                    ;
?>







http://www.bkjia.com/PHPjc/750859.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/750859.html

TechArticle

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP uses regular expressions to extract characters in angle brackets, parentheses, square brackets, and braces in a string_PHP TutorialNext article:PHP uses regular expressions to extract characters in angle brackets, parentheses, square brackets, and braces in a string_PHP Tutorial

Related articles

See more