三位的水仙花数共有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: 912985153
9: 472335975
9: 534494836
10: 4679307774
11: 32164049650
11: 40028394225
11: 42678290603
11: 49388550606
11: 32164049651
11: 94204591914
11: 44708635679
11: 82693916578
14: 28116440335967
16: 4338281769391370
16: 4338281769391371
17: 35875699062250035
17: 21897142587612075
17: 35641594208964132
19: 1517841543307505039
19: 3289582984443187032
19: 4929273885928088826
19: 4498128791164624869
20: 63105425988599693916
21: 449177399146038697307
21: 128468643043731391252
23: 27907865009977052567814
23: 35452590104031691935943
23: 27879694893054074471405
23: 28361281321319229463398
23: 21887696841122916288858
24: 174088005938065293023722
24: 239313664430041569350093
24: 188451485447897896036875 (为环保起见,24位以上的水仙花数略) 理论上,Z大的水仙花数不超过34位。
[编辑本段]求取方法(非高精度)
以下为在各种编程语言中实现求取水仙花数的方法(非高精度)。
◆PHP “水仙花数”实现代码:
◆C 语言的"水仙花数"实现代码:
#include
int main(void) { int a,b,c; for(a=1;a<=9;a++) for(b=0;b<=9;b++) for(c=0;c<=9;c++) if(100*a+10*b+c==a*a*a+b*b*b+c*c*c) printf("%d%d%d\n",a,b,c); return 0; }
◆PASCAL 实现代码:
program shuixianhuashu; 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 sxh; 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.◆Visual Basic 的"水仙花数"实现代码: Private Sub Form_Click() Dim a, b, c As Integer 'a(个)b(十)c(百) For a = 0 To 9 For b = 0 To 9 For c = 1 To 9 If a ^ 3 + b ^ 3 + c ^ 3 = a + 10 * b + 100 * c Then MsgBox 100 * c + 10 * b + a End If Next c Next b Next a End Sub◆FORTRAN 的"水仙花数"实现代码: WRITE(*,30) DO 10 K=100,999 IA=K/100 IB=MOD(K,100)/10 IC=MOD(K,10) IF(K.NE.IA**3+IB**3+IC**3) GOTO 10 WRITE(*,20)K, IA,IB,IC 10 CONTINUE 20 FORMAT(5X,4I4) 30 FORMAT(5X,18HN=I**3+J**3+K**3) STOP END
◆C++ 编译器上的水仙花数实现代码
#include using namespace std; int main() {int a,q,w,e; for(a=100;a<1000;++a) {q=a/100; w=(a-q*100)/10; e=(a-q*100-w*10); if(a==q*q*q+w*w*w+e*e*e) cout<
◆pyhon 中实现的代码
for i in range(1,10): for j in range(0,10): for k in range(0,10): if i*100+j*10+k==i*i*i+j*j*j+k*k*k: print i*100+j*10+k,
◆Java 中实现的代码
public class shuixianhuashu { public static void main(String[] args) { for(int i=100; i<1000; i++){ int a = i/100; int b = i/10%10; int c = i%10; if(Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3)==i) System.out.println(i+"是水仙花数"); } } }
◆C# ASP.NET 中的实现代码
for (int i = 100; i < 1000; i++) { int bai = 0; int shi = 0; int ge = 0; int baiyushu = 0; bai = i / 100; baiyushu = i % 100; shi = baiyushu / 10; ge = baiyushu % 10; if (i == bai * bai * bai + shi * shi * shi + ge * ge * ge) { Response.Write("水仙花数:" + i + "
"); } }
◆javascript +html 实现可变位数的运算:
无标题文档
◆asp 中实现的代码
<% dim a,b,c,d,m,n,z i=1 for i=100 to 999 a=mid(i,1,1) b=mid(i,2,1) c=mid(i,3,1) d=a*a*a m=b*b*b n=c*c*c z=d+m+n if z=i then response.write z & "
" end if next %>
◆Visual FoxPro 用表单实现法(只计3位)
(1)创建表单Form1并添加文本框Text1与命令按钮Command1 (2)修改Command1的Caption属性为“计算并显示” (3)为Form1添加方法sxh (4)修改方法sxh代码如下 para x x1=int(x%10) x2=int(x/10)%10 x3=int(x/100)%100 if x=x1^3+x2^3+x3^3 return .t. else return .f. endif (5)为Command1的Click事件编写如下的事件代码: thisform.currentx=thisform.width/2 thisform.currenty=thisform.height/2 thisform.print("水仙花数是:") for m=100 to 999 thisform.text1.value=m sure=thisform.sxh(m) if sure=.t. thisform.print(str(m,4)+space(3)) inkey(0.5) endif for 延迟=1 to 20000 yiru=2008610029 endfor endfor this.enabled=.f. QBASIC 水仙花数1—999999之间 For i = 1 To 999999 a = Mid(i, 1, 1) b = Mid(i, 2, 1) c = Mid(i, 3, 1) d = Mid(i, 4, 1) If i = a ^ 4 + b ^ 4 + c ^ 4 + d ^ 4 Then Print i; Next i
PB 实现的方法(只计3位数)
int s,a,b,c for s=100 to 999 a=integer(s/100) b=integer((s - a*100)/10) c=s - integer(s/10)*10 if s=a^3+b^3+c^3 then messagebox("",s) end if next
◆ActionScript实现的方法(只计3位数)
var n:int; var m:int; for (var i:int=1; i<=9; i++) { for (var j:int=1; i<=9; j++) { for (var k:int=1; i<=9; k++) { m=i*100+j*10+k; if (m==i*i*i+j*j*j+k*k*k) { n++; trace(m); } } } }
◆Delphi实现的方法(100-999)
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)) end end;
◆MATLAB中实现的方法(100-999)
for m=100:999 m1=fix(m/100); m2=rem(fix(m/10),10); m3=rem(m,10); if m==m1^3+m2^3+m3^3 disp(m) end end 或者:
◆Mathematica实现方法(可现不定位数解)
n=Input["请输入大于2的自然数n:"]; For[i=10^(n-1),i<10^n-1,i++, If[Total[IntegerDigits[i]^IntegerLength[i]]==i, Print[i]]]
◆添加一种C++的算法
#include #include using namespace std; void main() {int a,b,c,e,f,g; double d; b=1; f=0; a=100; e=0; c=g=a; A:do { a/=10; b++;} while(a>10); do{d=g%10; g/=10; e+=pow(d,b); f++;} while(f!=b+1); if(e==c) {cout<
◆BASH 脚本实现计算100-999之内数
#!/bin/bash for (( a=1; a<10; a++ )) do for (( b=0; b<10; b++ )) do for (( c=0; c<10; c++ )) do number1=$((a*100+b*10+c)) number2=$((a**3+b**3+c**3)) if [ $number1 -eq $number2 ]; then echo "Found number $number1" fi done done done