一:自幂数
一个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); } } 输出结果: