The number of daffodils refers to an n-digit (n>=3), in which the sum of the n powers of a number on each bit equals itself, and N is the number of digits. (for example: 1^3+5^3+3^3 = 153)
The number of daffodils is also called Armstrong.
The number of three-bit daffodils is 4:153,370,371,407
The number of four-bit daffodils is 3:1634,8208,9474
The number of five-bit daffodils is 3:54748,92727,93084
The number of six-bit daffodils is 1:548834
The number of seven-bit daffodils is 4:1741725,4210818,9800817,9926315
The number of eight-bit daffodils is 3:24678050,24678051,88593477
.....
The largest number of daffodils has 39 digits (115132219018763992565095597973971522401), and the number of total daffodils in the decimal natural number is 88.
The number of daffodils in PHP
1. The number of daffodils for the 3~7, find the number of daffodils
<?php//Poor lift Narcissus number function narcissistic ($n) {if ($n <3 | | $n >39) {return false;
//Save Execution result $result = Array ();
$start = POW ($n-1);
$end = POW ($n);
for ($i = $start; $i < $end; $i + +) {$total = 0;
$nums = Str_split ($i, 1);
foreach ($nums as $num) {$total + + = Pow ($num, $n);
} if ($total = = $i) {Array_push ($result, $i);
} return $result;
//Get current Microtime function Getmicrotime () {list ($usec, $sec) = Explode (", microtime ());
return (float) $usec + (float) $sec;
//Set timeout time of 3,600 seconds set_time_limit (3600);
Record starts running time $start = Getmicrotime ();
Executes the number of daffodils to find the 3~7 bit for ($i =3; $i <=7; $i + +) {$result [$i] = implode (', ', narcissistic ($i));
//Record run end time $end = Getmicrotime (); Output run time Echo ' run TimE: '. (float)
($end-$start);
Print result echo ' <pre> ';
Print_r ($result);
Echo ' </pre> '; ?>
Execution results:
Run time:82.230147838593
Array
(
[3] => 153,370,371,407
[4] => 1634,8208,9474
[5] => 54748,92727,93084
[6] => 548834
[7] => 1741725,4210818,9800817,9926315
)
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/
2. Optimization algorithm to find the number of daffodils, to find the number of daffodils 3~7 bit
Optimization method: Create a 0-9 N-order table for POW to reduce the number of calculations
<?php//Optimized narcissus number function narcissistic ($n) {if ($n <3 | | $n >39) {return false;
//Save Execution result $result = Array ();
N-Order pow corresponding Table $powlist = Getpow ($n);
$start = POW ($n-1);
$end = POW ($n);
for ($i = $start; $i < $end; $i + +) {$total = 0;
$nums = Str_split ($i, 1);
foreach ($nums as $num) {$total + = $powlist [$num];
} if ($total = = $i) {Array_push ($result, $i);
} return $result;
//Get current Microtime function Getmicrotime () {list ($usec, $sec) = Explode (", microtime ());
return (float) $usec + (float) $sec;
}//Get n-order POW corresponding table function Getpow ($n) {$powlist = array ();
For ($i =0 $i <=9; $i + +) {Array_push ($powlist, pow ($i, $n));
return $powlist; //Set timeout time of 3,600 seconds set_time_limit (3600);
Record starts running time $start = Getmicrotime ();
Executes the number of daffodils to find the 3~7 bit for ($i =3; $i <=7; $i + +) {$result [$i] = implode (', ', narcissistic ($i));
//Record run end time $end = Getmicrotime (); Output runtime echo ' Run Time: '. (float)
($end-$start);
Print result echo ' <pre> ';
Print_r ($result);
Echo ' </pre> '; ?>
Execution results:
Run time:47.354328155518
Array
(
[3] => 153,370,371,407
[4] => 1634,8208,9474
[5] => 54748,92727,93084
[6] => 548834
[7] => 1741725,4210818,9800817,9926315
)
As a result, the optimized algorithm reduces the execution time by 42%.