4 examples of how to implement Narcissus in PHP share _php tutorial

Source: Internet
Author: User
Example 1:
Copy CodeThe 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 code is as follows:
!--? PHP function cube ($n)
{
Return $n * $n * $n;
}

Function is_narcissistic ($n)
{
$hundreds = floor ($n/100);//Break out hundred
$tens = Floor ($n/10)% 10; Decompose the 10-bit
$ones = floor ($n% 10);//Break Out bits
Return (BOOL) (Cube ($hundreds) +cube ($tens) +cube ($ones) = = $n);
}


for ($i =, $i <, + + $i)
{
if (is_narcissistic ($i))
Echo $i. " \ n ";
}
?

Example 3:
Copy the Code code as follows:
Armstrong number: A K-digit, and the sum of the number of its digits on each bit is equal to its own. (Example: 1^3 + 5^3 + 3^3 = 153)
Class Armstrong {
static function index () {
for ($i = n, $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 the Code code as follows:



function Winter ($num)
{
if ($num <1000) {
Define Bits
$ge = $num% 10;
Define 10-bit
$ten = (($num%100)-$ge)/10;
Define the Hundred
/*floor rounding, ignoring all numbers after the decimal point */
$hundred =floor ($num/100);
$sum 1= $ge * $ge * $ge + $ten * $ten * $ten + $hundred * $hundred * $hundred;
if ($sum 1== $num) {
return 1;
} else{
return 0;
}

} else{
return-1;
}
}

if (Winter (371) ==-1) {
echo "number greater than 1000";
}else{
if (Winter (371)) {
echo "Yes";
}
else{
echo "No";
}
}

?>



http://www.bkjia.com/PHPjc/750859.html www.bkjia.com true http://www.bkjia.com/PHPjc/750859.html techarticle Example 1: Copy code 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 ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.