什么格式的变量可以装下50亿的阶乘?
用数组的方法来存储。。源码s源
以下是源码s源本人对大数阶乘的研究:
一个学计算机的哥们问的问题,用数组求阶乘。源码s源志愿填报 源码。源码s源。源码s源小说总站源码即为了避免溢出、源码s源以及显示出阶乘的源码s源每一位结果。。源码s源
经过思考,源码s源我觉得用矩阵相乘的源码s源原理来作比较简单。。源码s源
下面是源码s源python写源码在 Matlab 中写的求阶乘的代码:
function JC(n)
A=1;
q=1;
for i=1:1:n
tmp=i;
p=0;
z=0;
while tmp>0
p=p+1;
B(p)=tmp-fix(tmp/)*;
tmp=fix(tmp/);
end
bb=zeros(1,p);
for ii=1:1:p
bb(p+1-ii)=B(ii);
end
for jj=1:1:p
B(jj)=bb(jj);
end
H=zeros(p,p+q-1);
for a=1:1:p
for b=1:1:q
H(a,a+b-1)=B(a).*A(b);
end
end
D=zeros(1,p+q-1);
for c=1:1:p+q-1
for d=1:1:p
D(c)=D(c)+H(d,c);
end
end
E(p+q-1)=D(p+q-1);
for w=p+q-2:-1:1
E(w)=D(w)+fix(E(w+1)/);
end
A(1)=E(1);
for x=2:1:p+q-1
A(x)=E(x)-fix(E(x)/)*;
end
r=length(A)-1;
while A(1)>0
z=z+1;
F(z)=A(1)-fix(A(1)/)*;
A(1)=fix(A(1)/);
end
G=zeros(1,z+r);
for y=z+1:1:z+r
G(y)=A(y-z+1);
end
for v=1:1:z
G(v)=F(z-v+1);
end
for s=1:1:z+r
A(s)=G(s);
end
q=z+r;
end
d=0;
for i=q:-1:1
if A(i)==0
d=d+1;
else
break;
end
end
A
q
d
最后的结果:
A:以数组的方式显示出阶乘的结果
q:阶乘结果的位数
d:阶乘结果的尾部连续出现的0的个数。
下面是源码s源在 delphi 中的源码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, XPMan;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Edit1: TEdit;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
GroupBox2: TGroupBox;
GroupBox3: TGroupBox;
Edit2: TEdit;
GroupBox4: TGroupBox;
Edit3: TEdit;
XPManifest1: TXPManifest;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure Edit1KeyPress(Sender: TObject; var Key: Char);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{ $R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
Var
A,B,bb,D,E,F,G:array of integer;
C:array of array of integer;
N,i,ii,jj,LenA,LenB,LenT,tmp,r,Ze:integer;
S:string;
begin
Memo1.Clear;
LenA:=1;
Setlength(A,LenA);
A[0]:=1;
if Edit1.Text='' then
exit
else
begin
N:=strtoint(Edit1.Text);
for i:=1 to N do
begin
tmp:=i;
LenB:=0;
LenT:=0;
while tmp>0 do
begin
LenB:=LenB+1;
Setlength(B,lenB);
B[LenB-1]:=tmp-Trunc(tmp/)*;
tmp:=Trunc(tmp/);
end;
Setlength(bb,LenB);
for ii:=1 to lenB do
bb[LenB-ii]:=B[ii-1];
for ii:=1 to LenB do
B[ii-1]:=bb[ii-1];
Setlength(C,LenB,LenB+LenA-1);
for ii:=1 to LenB do
for jj:=1 to LenA+LenB-1 do
C[ii-1,jj-1]:=0;
for ii:=1 to LenB do
for jj:=1 to LenA do
C[ii-1,ii+jj-2]:=B[ii-1]*A[jj-1];
Setlength(D,LenB+LenA-1);
for ii:=1 to LenB+LenA-1 do
D[ii-1]:=0;
for ii:=1 to LenB+LenA-1 do
for jj:=1 to LenB do
D[ii-1]:=D[ii-1]+C[jj-1,ii-1];
Setlength(E,LenB+LenA-1);
E[LenB+LenA-2]:=D[LenB+LenA-2];
for ii:=LenB+LenA-2 downto 1 do
E[ii-1]:=D[ii-1]+Trunc(E[ii]/);
Setlength(A,LenB+LenA);
A[0]:=E[0];
for ii:=2 to LenB+LenA-1 do
A[ii-1]:=E[ii-1]-Trunc(E[ii-1]/)*;
r:=LenA+LenB-2;
while A[0]>0 do
begin
LenT:=LenT+1;
Setlength(F,LenT);
F[LenT-1]:=A[0]-Trunc(A[0]/)*;
A[0]:=Trunc(A[0]/);
end;
Setlength(G,lenT+r);
for ii:=LenT+1 to LenT+r do
G[ii-1]:=A[ii-LenT];
for ii:=1 to LenT do
G[ii-1]:=F[LenT-ii];
for ii:=1 to LenT+r do
A[ii-1]:=G[ii-1];
LenA:=LenT+r;
Setlength(A,LenA);
end;
Edit2.Text:=inttostr(LenA);
Ze:=0;
for i:=LenA downto 1 do
begin
if A[i-1]=0 then
Ze:=Ze+1
else
break;
end;
Edit3.Text:=inttostr(Ze);
S:='';
for i:=1 to LenA do
S:=S+inttostr(A[i-1]);
Memo1.Lines.Add(S);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Clear;
Edit1.Text:='';
Edit2.Text:='';
Edit3.Text:='';
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
Edit1.SetFocus;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if(key<>#8)and(key<>#)and(key<#)or(key>#)then
key:=#0;
if(key=#)then
Button1Click(sender);
end;
end.
Delphi 中用动态数组不像 Matlab那么方便。。源码s源但是抄linux源码运算速度比 Matlab 快了很多,基本上是Matlab 运算速度的4倍左右,在我的机子上(Pentuim M 1.7GHz,MB内存)计算的阶乘时Dephi 用了不到秒,Matlab 用了大概2分钟。threadlocal底层源码。
在百度中搜索:质数 用数组求阶乘
即可看到偶在“大富翁论坛”中发的帖子。。希望对你有帮助。。
2024-12-02 03:50
2024-12-02 03:36
2024-12-02 03:25
2024-12-02 01:21
2024-12-02 01:18