Home Backend Development PHP Tutorial 4 examples of how to implement the number of narcissus in PHP. Number of narcissus within 1000. Number of narcissus in vb c language to output narcissus.

4 examples of how to implement the number of narcissus in PHP. Number of narcissus within 1000. Number of narcissus in vb c language to output narcissus.

Jul 29, 2016 am 08:49 AM
php

Daffodil number refers to an n-digit number (n≥3), the sum of the nth power of the digits in each digit is equal to itself. (For example: 1^3 + 3^3+ 5^3 = 153) This article mainly introduces 4 examples of how to implement narcissus number in PHP. Friends who need it can refer to it:

Example 1, the code is as follows:

<?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>";
        }
      }
    }
}
?>
Copy after login

Example 2, the code is as follows:

<?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";
}
?>
Copy after login
Example 3, the code is as follows:

<?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 . &#39;<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();
Copy after login

Example 4, the code is as follows:

<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>
Copy after login

The above introduces 4 examples of how to implement the daffodil number in PHP, including the narcissus number and PHP content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles