Matlab 版 (精华区)

发信人: Idealguy (Idealguy), 信区: Matlab
标  题: Re: 发表一个算阶乘全部位数的matlab程序,Factoria
发信站: 哈工大紫丁香 (Sat Sep  1 14:33:29 2001) , 转信

【 在 Idealguy (Idealguy) 的大作中提到: 】
: 发表一个算阶乘全部位数的matlab程序,Factoria
: 使用方法: Factoria(n), n为自然数。 如 Factoria(2000)
: 程序如下。(将以下全部代码行存成文本文件 Factoria.uu) ,然后用 Winzip 打开 ..


贴出m文件:
----------------------------------------
function [S,id]=factoria(N)
% usage: factoria(n)
% Calculate the all digits of N!
% Idealguy,  2001.7.  in SHANGHAI
Base=100000; S='';
%  check if N is valid
% id=0--OK, 1--Dim error, 2--Range error
if prod(size(N))~=1, id=1; return, end
N=floor(N);
if (N<1) | (N>=Base), id=2; return, end
if N>2000 & upper(input('N>2000, will run very long! Continue?(Y/N)','s'))~='Y
', return, end
%
%  Guess the number of digits (on BASE)
M = floor(((N+0.5)*log(N+1)-N-0.081+1/(12*(N+1)))/log(Base)+1);
%
% Create and initialize Digits
F(1:M)=0; F(M)=1;
disp 'Please wait . . . . . .';
for i = 2:N
  T=F*i; R=floor(T/Base);
  F = T-floor(T/Base)*Base+[R(2:M),0];
end
%  Post process
T=floor(F/Base);
while any(T)
   F=F-floor(F/Base)*Base + [T(2:M),0];
   T=floor(F/Base);
end
id=0;
%change to strings
ns=ceil(M/10); m1=M-10*ns;
S1=sprintf('%5i,%05i,%05i,%05i,%05i,%05i,%05i,%05i,%05i,%05i,', F(1:m1+10));
S{1}=[ones(1,60-length(S1))*32 S1(1:end-1)];
for i=2:ns
   S{i}=sprintf('%05i,%05i,%05i,%05i,%05i,%05i,%05i,%05i,%05i,%05i',...
      F(max(1,m1+i*10-9):m1+i*10));
end
S=S(:);



--

※ 来源:.哈工大紫丁香 http://bbs.hit.edu.cn [FROM: 202.98.127.20]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:1.932毫秒