Java循环获取自幂数

文章介绍了自幂数的概念,包括三位数的水仙花数、四位数的四叶玫瑰数以及五位数的五角星数。提供了Java代码示例来计算这些特殊数字,展示了如何通过计算每个数位的幂并求和来验证是否为自幂数。
摘要由CSDN通过智能技术生成

一:自幂数

一个n位的自然数等于自身各个数位上的n次幂之和

如:153各个数为:1,5,3;对1,5,3分别立方再求和结果仍为153,所以153是一个自幂数

分类:三位数的自幂数称为水仙数,四位数为四叶玫瑰数,五位数为五角星数,等等

二:水仙数的代码实现

public class number {
    public static void main(String[] args) {

        //判断水仙数个数
        int count=0;

        //寻找水仙花数
        for(int i=100;i<1000;i++){

            //获取百分位的数
            double a=  Math.floor(i/100);

            //获取十分位的数
            double b= Math.floor((i%100)/10);

            //获取个分位的数
            int c=i%10;

            //判断
            if(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)==i) {
                System.out.println(i + "是水仙花数");
                count++;

            }

        }
        System.out.println("总共有"+count+"个水仙花数");
    }
}

输出结果:

三:四叶玫瑰数的代码实现

public class number2 {

    public static void main(String[] args) {

        //记录四叶玫瑰数的个数
        int count=0;

        for(int i=1000;i<10000;i++){
            //千分位上的数
            double a=Math.floor(i/1000);

            //百分位的数
            double b=Math.floor((i%1000)/100);

            //十分位的数
            double c=Math.floor((i%100)/10);

            //个分位的数
            int d=i%10;

            //判断
            if(Math.pow(a,4)+Math.pow(b,4)+Math.pow(c,4)+Math.pow(d,4)==i){
                System.out.println(i+"是四叶玫瑰数");
                count++;
            }
        }
        System.out.println("四叶玫瑰数的个数为:"+count);
    }
}

输出结果:

四:五角星数的代码实现

public class number3 {

    public static void main(String[] args) {

        //记录五角星数的个数
        int count=0;

        for(int i=10000;i<100000;i++){

            //万位数
            double a=Math.floor(i/10000);

            //千分位
            double b=Math.floor((i%10000)/1000);

            //百分位
            double c=Math.floor((i%1000)/100);

            //十分位
            double d=Math.floor((i%100)/10);

            //个分位
            int e=i%10;

            //判断
            if(Math.pow(a,5)+Math.pow(b,5)+Math.pow(c,5)+Math.pow(d,5)+Math.pow(e,5)==i){
                System.out.println(i+"是五角星数");
                count++;
            }
        }
        System.out.println("五角星数的个数为:"+count);
    }
}
输出结果:

  • 20
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值