水仙花數(Narcissistic number)也被稱為超完全數字不變數(pluperfect digital invariant, PPDI)、自戀數、自冪數、阿姆斯壯數或阿姆斯特朗數(Armstrong number),水仙花數是指一個 3 位數,它的每個位上的數字的 3次冪之和等於它本身(例如:1^3 + 5^3+ 3^3 = 153)。
基本介紹
- 中文名:水仙花數
- 外文名:narcissistic number
- 別名:pluperfect digital invariant
- 別名:Armstrong number
- 學科:數學、計算機
基本信息
定義
常見水仙花數
求取方法
方法一:def narcissistic_number_1(num): length = len(str(num)) count = length num_sum = 0 while count: num_sum += ((num // 10 ** (count - 1)) % 10) ** length count -= 1 else: if num_sum == num: print("%d is %d bit narcissistic_number" % (num, length)) else: print("%d is not a narcissistic_number" % num) 方法二:def narcissistic_number_2(num): original_num = num s = str(original_num) length = len(s) count = length sum_num = 0 while count: sum_num += int(s[count - 1]) ** length count -= 1 else: if sum_num == num: print("%d is a %d bit narcissistic_number" % (num, length)) else: print("%d is not a narcissistic_number" % num) max_num = int(input('請輸入最大範圍'))# 獲取小於指定數的阿姆斯特朗數for num in range(0, max_num): narcissistic_number_1(num) #調用方法一,方法二均可
begin integer i,j,k; for i := 1 step 1 until 9 do for j := 0 step 1 until 9 do for k := 0 step 1 until 9 do if i×i×i + j×j×j + k×k×k = 100×i + 10×j + k then print (100×i + 10×j + k);end
<?php//使用系統自帶函式,即可實現冪運算,何必畫蛇添足,自己寫函式functionis_narcissistic($n){$hundreds=floor($n/100);//分解出百位$tens=floor($n/10)%10;//分解出十位$ones=floor($n%10);//分解出個位return(bool)(pow($hundreds,3)+pow($tens,3)+pow($ones,3)==$n);}for($i=100;$i<1000;++$i){if(is_narcissistic($i))echo$i."\n";}die();?>
<?php/***水仙花數為不小於3位的數字,每位數字的N次冪的和等於該數字.N為該數字的位數*@namedaffodilsNum水仙花數*@param$places水仙花位數>=3*/functiondaffodilsNum($places=3){//定義位數if(!defined('PLACES'))define('PLACES',is_numeric($places)?$places:3);if(PLACES>=3){$min=pow(10,PLACES-1);//選數範圍起始位置$max=pow(10,PLACES);//選數範圍結束位置//開始選數for($i=$min,$out='';$i<$max;$i++){$sum=0;//當前選數下各個冪值的和$arr=str_split($i);//以字元串方式分割選數for($j=0;$j<PLACES;++$j)//對每個數字作冪操作並累加{$sum+=pow($arr[$j],PLACES);if($sum>$i)//如果當前累加已大於選數,則跳出循環break;}if($sum==$i)//如果符合定義,將該數字添加到輸出佇列$out.=$i."\n";}//輸出佇列echo$out;}}echodaffodilsNum(3);?>
program narcissistic_number;var a,b,c:integer;begin for a:=1 to 9 do for b:=0 to 9 do for c:=0 to 9 do if a*a*a+b*b*b+c*c*c=100*a+10*b+c then writeln(100*a+10*b+c);end.或program narcissistic_number;var a,b,c,d:integer;begin for a:=100 to 999 do begin b:=a mod 10; c:=a mod 100 div 10; d:=a div 100; if b*b*b+c*c*c+d*d*d=a then writeln(a); end;end.或program narcissistic_number;var a, b, c, i, t : integer;begin i := 100; repeat a:=trunc(i/100); b:=trunc(i/10) - a*10; c:=i-trunc(i/10) * 10; t:= a*a*a + b*b*b + c*c*c; if i = t then writeln(i,'=',a,'^3+',b,'^3+',c,'^3'); i := i + 1 until i > 999end.
Private Sub Command1_Click()Dim i As Integer, a As Integer, b As Integer, c As IntegerFor i = 100 To 999 Step 1 a = i \ 100 b = (i - 100 * a) \ 10 c = i - 100 * a - 10 * b If a^(3) + b^(3) + c^(3) = i Then Print i End If Next iEnd Sub
Dim a As Integer, b As Integer, c As Integer For a = 0 To 9 For b = 0 To 9 For c = 0 To 9 If a * 100 + b * 10 + c = a ^ 3 + b ^ 3 + c ^ 3 Then Console.WriteLine(a * 100 + b * 10 + c) End If Next Next Next
sumnum = 0FOR a = 3 TO 7?ALLTRIM(STR(a))+"位的水仙花數有: " FOR i=10^(a-1) TO 10^a-1 FOR m = 1 TO a n = SUBSTR(ALLTRIM(STR(i)),m,1) sumnum = sumnum+VAL(n)^a ENDFOR IF i = sumnum ??STR(i,a,0)+" " ENDIF sumnum = 0 ENDFORENDFOR
#include<stdio.h>#include<stdlib.h>#include<stdbool.h>int cube(const int n){return n*n*n;}boolisNarcissistic(const int n){int hundreds=n/100;int tens=n/10-hundreds*10;int ones=n%10;return cube(hundreds)+cube(tens)+cube(ones)==n;}int main(void){int i;for(i=100;i<1000;++i){if(isNarcissistic(i))printf("%d\n",i);}return EXIT_SUCCESS;}
#include <stdio.h> #include <stdlib.h>void main() { int i,j,k,n; printf("'water flower'number is:"); for(n=100;n<1000;n++) { i=n/100;/*分解出百位*/ j=n/10%10;/*分解出十位*/ k=n%10;/*分解出個位*/ if(n==i*i*i+j*j*j+k*k*k) { printf("%-5d",n); } } printf("\n"); }
#include<iostream>using std::cout;using std::endl;#define f(a) (a)*(a)*(a)int main() { for (int i = 100;i < 1000;i++) if (f(i % 10) + f(i / 10 % 10) + f(i / 100 % 10) == i) cout << i << endl; system("pause"); return 0;}
#include<iostream>#include <cmath>using namespace std;int _tmain(int argc, _TCHAR* argv[]){ long n1, n2, a; int i; cout << "請輸入Narcissistic number的位數:" << endl; cin >> i; cout << i << "位數的Narcissistic number包括:" << endl; for (n1 = pow(10, i - 1); n1 < pow(10, i); n1++) { n2 = 0; for (int j=0; j < i; j++) { a = pow(10, j); a = n1 / a; a = a % 10; a = pow(a, i); n2 = n2 + a; } if (n1 == n2) cout << n1 << endl; } return 0;}
[X*100+Y*10+Z||X<-lists:seq(1,9),Y<-lists:seq(0,9),Z<-lists:seq(0,9),X*X*X+Y*Y*Y+Z*Z*Z=:=X*100+Y*10+Z].
#!/usr/bin/envpythonfor i in range(1,10): for j in range(0,10): for k in range(0,10): if i*100+j*10+k==i**3+j**3+k**3: print(i*100+j*10+k)
fun main(args: Array<String>) { val n = 5 println(factorial1(n)) println(factorial2(n)) (100..999).filter { val r = it / 100 val s = (it - r * 100) / 10 val t = it - r * 100 - s * 10 it == r * r * r + s * s * s + t * t * t }.forEach{ println(it)}}
package sy1;import java.util.Scanner;public class TestNarcissus { /* * public static void main(String[] args) { * int num = 100; int a[] = new int[3]; * System.out.print("3位的水仙花數有:\t"); * while (num <= 999) { * int sum =0; * a[0] = num / 100 % 10; * a[1] = num / 10 % 10; * a[2] = num % 10; * for (int i = 0; i < 3; i++) { * sum = sum + (int) Math.pow(a[i], 3); * } * if (num ==sum) { * System.out.print(num + "\t"); } num++; * } * } */ public static void main(String[] agrs) { System.out.print("指定最大位數N:"); Scanner input = new Scanner(System.in); int N = input.nextInt(); input.close(); for (int i = 3; i <= N; i++) { int a[] = new int[i]; int num = (int) Math.pow(10, i - 1) + 1; System.out.print(i + "位的水仙花數有:\t"); while (num <= Math.pow(10, i)) { int sum = 0; for (int j = 0; j < i; j++) a[j] = (int) (num / Math.pow(10, j) % 10); for (int j = 0; j < i; j++) sum = sum + (int) Math.pow(a[j], i); if (num == sum) System.out.print(num + "\t"); num++; } System.out.print("\n"); } }}
for(inti=100;i<1000;i++){intbai=0;intshi=0;intge=0;intbaiyushu=0;bai=i/100;baiyushu=i%100;shi=baiyushu/10;ge=baiyushu%10;if(i==bai*bai*bai+shi*shi*shi+ge*ge*ge){Console.WriteLine("水仙花數:"+i+"<br>");}}
補充C#水仙花數實現代碼(不定位數)/// <summary>/// 判斷一個數是否是水仙花數/// </summary>/// <param name="num">要判斷的數</param>/// <returns>判斷結果:true-是,false-否</returns>bool isWaterFlower(int num){if (num <= 0){return false;}int temp = num;//將要判斷的數值各位上的數字拆開放在集合中ArrayList list = new ArrayList(); while (temp > 0) { list.Add(temp % 10);temp /= 10;}//判斷各位上的數字位數次方之後是否等於要判斷是數,是的話則為水仙花數 int sum = 0; foreach (int i in list) { int mul = 1; for (int j = 0; j < list.Count; j++){mul *= i;} sum += mul; } return sum == num;}
補充C#中1000以內水仙花數實現代碼(同理可根據需求自行增加變數及循環次數,此處僅為舉例)int number = 100; //聲明變數number初始值為100for(int i=1;i<10;i++) //聲明百位數的值變數為 i,賦值為1,每次循環加1{ for(int j=0;j<10;j++) //聲明十位數的值變數為j,賦值為0,每次循環加1 { for(int r=0;r<10;r++) //聲明個位數的值為r,賦值為0,每次循環加1 { if(i*i*i+j*j*j+r*r*r==number) { Console.WriteLine("水仙花數:{0}", number); } number++; //number加1 } }}
let power3 x= x*x*xlet findNarcissisticNumber ()=seq {100..999}|> Seq.where (fun num->let x= num/100 //百位let y= num%100/10 //十位let z= num%10 //個位power3 x + power3 y + power3 z = num)let narcissisticNumbers= findNarcissisticNumber()printfn "Narcissistic Numbers :%A" narcissisticNumbers
<!DOCTYPE html><html> <head> <meta charset="utf-8"> </head> <body> <script type="text/javascript"> var a = 0, b = 0, c = 0; for (var i = 100; i < 1000; i++) { a = i % 10; b = parseInt(((i / 10) % 10)); c = parseInt(i / 100); if (i === a * a * a + b * b * b + c * c * c) { document.write('水仙花數: ' + i + '<br/>'); } } </script> </body></html>
<%dim a,b,c,d,m,n,zi=1for i=100 to 999a=mid(i,1,1)b=mid(i,2,1)c=mid(i,3,1)d=a*a*am=b*b*bn=c*c*cz=d+m+nif z=i thenresponse.write z & "<br>"end ifnext%>Visual FoxPro 用表單實現法(只計3位)方法一:clearfor a=1 to 9for b=0 to 9for c=0 to 9x=a*100+b*10+cif x=a^3+b^3+c^3?xendifendforendforendfor方法二:(1)創建表單Form1並添加文本框Text1與命令按鈕Command1(2)修改Command1的Caption屬性為“計算並顯示”(3)為Form1添加方法sxh(4)修改方法sxh代碼如下para xx1=int(x%10)x2=int(x/10)%10x3=int(x/100)%100if x=x1^3+x2^3+x3^3return .t.elsereturn .f.endif(5)為Command1的Click事件編寫如下的事件代碼:thisform.currentx=thisform.width/2thisform.currenty=thisform.height/2thisform.print("水仙花數是:")for m=100 to 999thisform.text1.value=msure=thisform.sxh(m)if sure=.t.thisform.print(str(m,4)+space(3))inkey(0.5)endiffor 延遲=1 to 20000yiru=2008610029endforendforthis.enabled=.f.
REM Print all the Narcissistic numberREM between 1 and 999999FOR i = 1 TO 999999 e$ = STR$(i) a$ = MID(e$, 1, 1) b$ = MID(e$, 2, 1) c$ = MID(e$, 3, 1) d$ = MID(e$, 4, 1) a = VAL(a$) AND b = VAL(b$) AND c = VAL(c$) AND d = VAL(d$) IF i = a ^ 4 + b ^ 4 + c ^ 4 + d ^ 4 THEN PRINT i;NEXT iENDPB 實現的方法(只計3位數)int s,a,b,cfor s=100 to 999a=integer(s/100)b=integer((s - a*100)/10)c=s - integer(s/10)*10if s=a^3+b^3+c^3 thenmessagebox("",s)end ifnext
var a,b,c,d:integer;begin for a:=100 to 999 do begin b := a div 100; c := a div 10 mod 10; d := a mod 10; if b*b*b+c*c*c+d*d*d=a then memo1.Lines.Add(inttostr(a)) endend;
for m=100:999m1=fix(m/100);m2=rem(fix(m/10),10);m3=rem(m,10);if m==m1^3+m2^3+m3^3disp(m)endend
m = 100:999;n = floor(m/100).^3+mod(fix(m/10),10).^3+mod(m,10).^3;m(find(m==n))
func sxhs3(start:Int,end:Int) -> [Int]{ var value = [Int]() guard start < end else { return value } for i in start ... end { let a = i / 100 let b = i % 100 / 10 let c = i % 10 if a * a * a + b * b * b + c * c * c == i{ value.append(i) } } return value}
package mainimport ( "fmt" "strconv")func main() { var a, b int64 fmt.Println("請輸入求a,b:") fmt.Scanf("%d,%d", &a, &b) if a > b { a, b = b, a } for i := a; i <= b; i++ { if isNarcissusFew(i) { fmt.Println(i) } }}func isNarcissusFew(number int64) bool { var sum int64 numberStr := strconv.FormatInt(number, 10) for _, data := range numberStr { lenght := len(numberStr) num := (int64)(data - 48) var sum1 int64 = 1 for lenght != 0 { sum1 *= num lenght-- } sum += sum1 } if sum == number { return true } return false}
function IsNarcLNum(num) local lenth = string.len(tostring(num)) local sumval = 0 num = math.modf(num) local data = num while(data > 0 ) do sumval = sumval + (math.fmod(data, 10)^lenth) data = math.modf(data/10) end if sumval == num then return true else return false end endval = 10000for i = 1, val do if IsNarcLNum(i) then print(string.format("%d", i)) end end
stack segment stackdw 256 dup (?)stack endsdata segmentbuf db 3 dup (?)data endscode segmentassume cs:code,ds:data,ss:stackfj proc nearpush cxpush dxpush simov si,0mov cx,10fj1:mov dx,0div cxmov buf[si],dlinc sior ax,axjnz fj1mov ax,sipop sipop dxpop cxretfj endpprint proc nearpush cxpush dxmov dx,-1push dxmov cx,10p1:mov dx,0div cxpush dxor ax,axjnz p1p2:pop dxcmp dx,-1je p9add dl,30hmov ah,2int 21hjmp p2p9:mov dl,9mov ah,2int 21hpop dxpop cxretprint endpstart:mov ax,datamov ds,axmov bx,100s0:mov ax,bxcall fjmov cx,axmov si,0mov dx,0s1:mov al,buf[si]mul buf[si]mul buf[si]add dx,axinc siloop s1cmp dx,bxjne s2mov ax,dxcall prints2:inc bxcmp bx,1000jb s0mov ah,4chint 21hcode endsend start