• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

php实现水仙花数(推荐方法一和五)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身。(例如:1^3 + 3^3+ 5^3 = 153)

方法一、

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

方法二、

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

方法三、

<?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 . '<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();

方法四、

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

方法五、

<?php 
header('content-type:text/html;charset=utf8 ');
 ?>
 <script>
for (var i=100; i < 1000; i++) { 
    a=parseInt(i%10);//分解出个位
    b=parseInt(i/10%10);//分解出十位
    c=parseInt(i/100%10);//分解出百位
    if(c*c*c+b*b*b+a*a*a==i){
        document.write(i+"<br>");
    }
}
 </script>

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap