qibbxxt 发表于 2010-9-9 15:35

关于一些有趣的数字的编程实现

本帖最后由 qibbxxt 于 2010-9-9 15:37 编辑

最近编写了关于水仙花等数的程序,写了一个gui,发在这里,大家一起讨论,学习


核心代码如下:
for i=str_n:en_n
a=10^(i-1):(10^i-1);
b=0:(i-1);
y0=mod(floor(bsxfun(@rdivide,repmat(a',1,i),10.^fliplr(b))),10);
switch val
case 1 % 阿姆斯特朗数
y=sum(y0.^i,2);
case 2 % 3
y=sum(bsxfun(@power,y0,y0),2);
otherwise % The numbers that are equal to the sum of consecutive powers of their digits
y=sum(bsxfun(@power,y0,1:i),2);
end
y=y(a'==y);
yy=;
end
1、阿姆斯特朗数
PPDI (Armstrong) Numbers
153 = 1^3 + 5^3 + 3^3
1634 = 1^4 + 6^4 + 3^4 + 4^4
54748 = 5^5 + 4^5 + 7^5 + 4^5 + 8^5
548834 = 5^6 + 4^6 + 8^6 + 8^6 + 3^6 + 4^6
1741725 = 1^7 + 7^7 + 4^7 + 1^7 + 7^7 + 2^7 + 5^7
24678050 = 2^8 + 4^8 + 6^8 + 7^8 + 8^8 + 0^8 +5^8 + 0^8
146511208 = 1^9 + 4^9 + 6^9 + 5^9 + 1^9 + 1^9 + 2^9 + 0^9 + 8^9
4679307774 = 4^10 + 6^10 + 7^10 + 9^10 + 3^10 + 0^10 + 7^10 + 7^10 + 7^10 + 4^10
82693916578 = 8^11 + 2^11 + 6^11 + 9^11 + 3^11 + 9^11 + 1^11 + 6^11 + 5^11 + 7^11 + 8^11
3、
3435 =3^3 +4^4 +3^3 +5^5
438579088 =4^4 +3^3 +8^8 +5^5 +7^7 +9^9 +0^0 +8^8 +8^8
...
4、The numbers that are equal to the sum of consecutive powers of their digits
如:
89 = 8^1 + 9^2
135 = 1^1 + 3^2 + 5^3
...
2646798 = 2^1 + 6^2 + 4^3 + 6^4 + 7^5 + 9^6 + 8^7
这是由仿真科技的lin2009提供
页: [1]
查看完整版本: 关于一些有趣的数字的编程实现