複製鏈接
請複製以下鏈接發送給好友

自冪數

鎖定
如果在一個固定的進制中,一個n位自然數等於自身各個數位上數字的n次冪之和,則稱此數為自冪數。
例如:在十進制中,153是一個三位數,各個數位的3次冪之和為1^3+5^3+3^3=153,所以153是十進制中的自冪數。
在n進制中,所有小於n的正整數都為自冪數,比如2進制中1是自冪數,3進制中1和2都是自冪數,4進制中1,2和3都是自冪數......
中文名
自冪數
外文名
Narcissistic number
説    明
自冪數是指一個 n 位數
n為1時
自冪數稱為獨身數
各種自冪數的名稱
一位自冪數:獨身數
兩位自冪數:沒有
三位自冪數:水仙花數
四位自冪數:四葉玫瑰數
五位自冪數:五角星數
六位自冪數:六合數
七位自冪數:北斗七星數
八位自冪數:八仙數
九位自冪數:九九重陽數
十位自冪數:十全十美數 [1] 

自冪數較小自冪數

獨身數共有9個: 1,2,3,4,5,6,7,8,9;
水仙花數共有4個:153,370,371,407;
四葉玫瑰數共有3個:1634,8208,9474;
五角星數共有3個:54748,92727,93084;
六合數只有1個:548834;
北斗七星數共有4個:1741725,4210818,9800817,9926315;
八仙數共有3個:24678050,24678051,88593477
……
使用高精度計算,可以得到超過int類型上限的水仙花數:
5: 93084
5: 92727
5: 54748
6: 548834
7: 9800817
7: 4210818
7: 1741725
7: 9926315
8: 24678050
8: 24678051
8: 88593477
9: 146511208
9: 472335975
9: 534494836
9: 912985153
10: 4679307774
11: 32164049650
11: 32164049651
11: 40028394225
11: 42678290603
11: 44708635679
11: 49388550606
11: 82693916578
11: 94204591914
14: 28116440335967
16: 4338281769391370
16: 4338281769391371
17: 21897142587612075
17: 35641594208964132
17: 35875699062250035
19: 1517841543307505039
19: 3289582984443187032
19: 4929273885928088826
19: 4498128791164624869
20: 63105425988599693916
21: 449177399146038697307
21: 128468643043731391252
23: 27907865009977052567814
23: 35452590104031691935943
23: 27879694893054074471405
23: 21887696841122916288858
24: 174088005938065293023722
24: 188451485447897896036875
(為環保起見,24位以上的自冪數略)
十進制中最大的自冪數有39位,共有88個自冪數。

自冪數實現代碼

C參考代碼
//方法一(輸入n(3<=n<=9),輸出n位自冪數)
#include <stdio.h>
#include <math.h>
/*n位數拆分*/
void func(int num, int* nums, int nums_size){
    div_t x={.quot=num, .rem=0};
    for(int i=0;i<nums_size;++i){
        x=div(x.quot, 10);//商再除以10
        nums[i]=x.rem;//餘數
    }
}
/*n個數的n次冪的和*/
int power_sum(int* nums, const int nums_size){
    int ret=0;
    int tem=0;
    for(int i=0;i<nums_size;++i){
        tem=nums[i];
        for(int j=1; j<nums_size;++j){
            tem*=nums[i];
        }
        ret+=tem;
    }
    return ret;
}
int main(void)
{ 
    int n,a,b,nums[7];
    scanf("%d",&n);
    if(n<3||n>9){
        printf("n太大");
        return 0;
    }
    a=pow(10,n-1);//下界
    b=a*10;//上界
    for(int i=a; i<b; i++)
    { 
        func(i, nums, n); 
        if(i==power_sum(nums, n))
        printf("%d ",i);
    }
    return 0; 
}
//方法二(輸入n(3<=n<=7),輸出n位自冪數)
#include <stdio.h>
#include <stdlib.h>
int main(void)
 {
 int n,i,a,b,c1,c2,c3,c4,c5,c6,c7;
 scanf("%d",&n);
 a=pow(10,n-1);
 b=pow(10,n);
 if(n==3)
 for(i=a; i<b; i++)
 {
 c1=i%10;
 c2=i/10%10;
 c3=i/100;
 if(i==pow(c1,3)+pow(c2,3)+pow(c3,3))
 printf("%d ",i);
 }
 else if(n==4)
 for(i=a; i<b; i++)
 {
 c1=i%10;
 c2=i/10%10;
 c3=i/100%10;
 c4=i/1000;
 if(i==c1*c1*c1*c1+c2*c2*c2*c2+c3*c3*c3*c3+c4*c4*c4*c4)
 printf("%d ",i);
 }
 else if(n==5)
 for(i=a; i<b; i++)
 {
 c1=i%10;
 c2=i/10%10;
 c3=i/100%10;
 c4=i/1000%10;
 c5=i/10000;
 if(i==c1*c1*c1*c1*c1+c2*c2*c2*c2*c2+c3*c3*c3*c3*c3+c4*c4*c4*c4*c4+c5*c5*c5*c5*c5)
 printf("%d ",i);
 }
 else if(n==6)
 for(i=a; i<b; i++)
 {
 c1=i%10;
 c2=i/10%10;
 c3=i/100%10;
 c4=i/1000%10;
 c5=i/10000%10;
 c6=i/100000;
 if(i==c1*c1*c1*c1*c1*c1+c2*c2*c2*c2*c2*c2+c3*c3*c3*c3*c3*c3+c4*c4*c4*c4*c4*c4+c5*c5*c5*c5*c5*c5+c6*c6*c6*c6*c6*c6)
 printf("%d ",i);
 }
 else if(n==7)
 for(i=a; i<b; i++)
 {
 c1=i%10;
 c2=i/10%10;
 c3=i/100%10;
 c4=i/1000%10;
 c5=i/10000%10;
 c6=i/100000%10;
 c7=i/1000000;
 if(i==c1*c1*c1*c1*c1*c1*c1+c2*c2*c2*c2*c2*c2*c2+c3*c3*c3*c3*c3*c3*c3+c4*c4*c4*c4*c4*c4*c4+c5*c5*c5*c5*c5*c5*c5+c6*c6*c6*c6*c6*c6*c6+c7*c7*c7*c7*c7*c7*c7)
 printf("%d ",i);
 }
     else printf("Error!");
 return 0;
 }

C++參考代碼
##include<iostream>
#include<cmath>
using namespace std;
int main()
{
    int n;
    int start, end;
    int i;
    int m;
    int digit;
    int sum;

    cout << "計算自冪數,請輸入位數:";
    cin >> n;
    while (n >= 1)
    {
        start = pow(10, n - 1);
        end = pow(10, n);
        cout << n << "位自冪數:" ;
        for (i = start; i < end; i++)
        {
            m = i;
            sum = 0;
            while (m != 0)
            {
                digit = m % 10;
                sum = sum + pow(digit, n);
                m = m / 10;
            }
            if (i == sum)
                cout << i << " ";
        }
        cout << endl;
        cout << "求n位自冪數,請輸入位數:";
        cin >> n;
    }
    cout << endl;
    return 0;
}
Java參考代碼
public class Exp2 {
    public static void main(String args[]) {
        Math1 m = new Math1();
        int x=99999999;//遍歷10~99999999中的自冪數
        int k=0;
        for (int i = 10; i <= x; i++){
            if(i%(x/50)==0){    //打印遍歷完成進度。
                k=k+2;
                System.out.print(k+"%   ");
            }
                
            if (m.mishu(i) == true)
                System.out.println(i);
        }
    }
}
class Math1 {
    public boolean mishu(int x) {
        int k = 0;
        int[] s = chaifen(x);
        for (int i = 0; i < s.length; i++) {
            k=k+pingfang(s[i],s.length);
        }
        if(x==k)
        return true;
        else
        return false;
    }
    public int pingfang(int x, int y) {
        x = (int) Math.pow(x, y);
        // System.out.println(x);
        return x;
    }
    public int[] chaifen(int x) {
        int l = String.valueOf(x).length();
        int[] s = new int[l];
        for (int i = 0; i < l; i++) {
            // System.out.println("--------"+ (pingfang(10,(l-i-1))));
            s[i] = x / (pingfang(10, (l - i - 1))) % 10;
        }
        return s;
    }
}
輸出結果:
153
370
371
407
1634
8208
9474
54748
92727
93084
548834
1741725
2%   4%   4210818
6%   8%   9800817
9926315
10%   12%   14%   16%   18%   20%   22%   24%   24678050
24678051
26%   28%   30%   32%   34%   36%   38%   40%   42%   44%   46%   48%   50%   52%   54%   56%   58%   60%   62%   64%   66%   68%   70%   72%   74%   76%   78%   80%   82%   84%   86%   88%   88593477
90%   92%   94%   96%   98%   100%   
Python參考代碼
n = int(input())
########## Begin ##########
if n==1:
    print(0)
X1=10**(n-1)
X2=10**(n)
for xi in range(X1,X2):
    L=[int(i) for i in(str(xi))]
    if xi==sum([m**n for m in L]):
        print(xi)
########## End ##########
參考資料