PHP print narcissus number function code

WBOY
Release: 2016-07-25 08:53:36
Original
1956 people have browsed it
  1. php daffodil number function--bbs.it-home.org
  2. < ;body>
  3. function winter($num)
  4. {
  5. if($num<1000){
  6. //Define the ones digit
  7. $ge=$num%10;
  8. //Define the tens digit
  9. $ten =(($num%100)-$ge) /10;
  10. //Define the hundreds place
  11. /*floor rounding, ignore all numbers after the decimal point*/
  12. $hundred=floor($num/100);
  13. $ sum1=$ge*$ge*$ge+$ten*$ten*$ten+$hundred*$hundred*$hundred;
  14. if($sum1==$num){
  15. return 1;
  16. } else{
  17. return 0;
  18. }

  19. } else{

  20. return -1;
  21. }
  22. }

  23. if(winter(371)==-1) {

  24. echo "greater than The number of 1000";
  25. }else{
  26. if(winter(371)) {
  27. echo "Yes";
  28. } else{
  29. echo "No";
  30. }
  31. }
  32. ?>
  33. < ;/html>

Copy code

Example 2, PHP implements narcissus number

  1. for($i=0;$i<1000;$i++)
  2. {
  3. $a=floor($i/100);//Find the hundreds digit
  4. $b =floor($i/10)%10;//Find the tens digit
  5. $c=$i%10;//Find the ones digit
  6. //if($a*$a*$a+$b* $b*$b+$c*$c*$c==$i)
  7. 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
  8. {
  9. echo $i."
    ";
  10. }
  11. }
  12. ?>
Copy code

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!