declare @t int set @T = 10 --计算10的阶乘 SET NOCOUNT ON declare @ta table(id int identity(1,1),col bigint)insert @ta(col) select 1 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 insert @ta(col) select 0 declare @n int,@i int,@c bigint,@len int,@tmp bigint,@tp bigintset @len=1; set @n = @t while (@n >=1) begin select @c = 0 ,@i = 1 select @tmp = col from @ta where id = @i while (@i <= @len) begin set @tmp = @tmp*@n+@c set @tp = @tmp set @tmp = @tmp % 10000; update @ta set col = @tmp where id = @i; set @c = @tp / 10000 set @i = @i + 1 select @tmp = col from @ta where id = @i end update @ta set col = @c where id = @i; if (@c>0) set @len = @len + 1 set @n = @n - 1 end select * from @taSET NOCOUNT Off有人请客吃饭,来回来再把@ta的初始行数计算出来就可以1000
declare @t int set @T = 40 --计算40的阶乘 SET NOCOUNT ON declare @ta table(id int identity(1,1),col bigint) insert @ta(col) select 1declare @n int,@i int,@c bigint,@len int,@tmp bigint,@tp bigintset @len=1; set @n = @t while (@n >=1) begin select @c = 0 ,@i = 1 select @tmp = col from @ta where id = @i while (@i <= @len) begin set @tmp = @tmp*@n+@c set @tp = @tmp set @tmp = @tmp % 10000;
update @ta set col = @tmp where id = @i; set @c = @tp / 10000 set @i = @i + 1 if not exists(select 1 from @ta where id = @i) insert @ta (col) select 0 select @tmp = col from @ta where id = @i end update @ta set col = @c where id = @i; if (@c>0) set @len = @len + 1 set @n = @n - 1 end declare @s varchar(8000)
select @s = isnull(@s+' ','')+right('0000'+ltrim(col),4) from @ta order by id desc select @sSET NOCOUNT Off/* ---------------------------------------------------------------- 0000 8159 1528 3247 8977 3434 5611 2695 9611 5894 2720 0000 0000 */对@ta自行判断要不生成行哪个测试一下1000 , 嘿嘿,吃饭去了
既然是SQLSERVER,为什么不用表呢? create table tb(id int IDENTITY(1,1) PRIMARY KEY,factor bigint) insert into tb(factor) values(1) go declare @i int,@factormax int set @i=2 while @i<=1000 begin update tb set factor=factor*@i update tb set factor=factor+isnull((select floor(factor/1000000000000000) from tb where id=a.id-1),0) from tb a select @factormax=floor(factor/1000000000000000) from tb where factor>=1000000000000000 and id=(select top 1 id from tb order by id desc) if @factormax is not null insert into tb(factor) values(@factormax) update tb set factor=factor % 1000000000000000 set @i=@i+1 set @factormax=null end select right('000000000000000'+convert(varchar,factor),15) from tb order by id desc go drop table tb 运行时间:5秒结果大家自己看.
加一句SET NOCOUNT ON; 9000的阶乘,用时7分01秒.
程序还可以精练一点: create table tb(id int IDENTITY(1,1) PRIMARY KEY,factor bigint) insert into tb(factor) values(1) go SET NOCOUNT ON; declare @i int,@factormax int set @i=2 while @i<=1000 begin update tb set factor=factor*@i update tb set factor=factor+isnull((select floor(factor/1000000000000000) from tb where id=a.id-1),0) from tb a select @factormax=floor(factor/1000000000000000) from tb where id=(select top 1 id from tb order by id desc) if @factormax >0 insert into tb(factor) values(@factormax) update tb set factor=factor % 1000000000000000 set @i=@i+1 end select right('000000000000000'+convert(varchar,factor),15) from tb order by id desc go drop table tb 1000的阶乘,运行时间2秒.
Microsoft SQL Server Management Studio 9.00.3042.00 Microsoft Analysis Services 客户端工具 2005.090.3042.00 Microsoft 数据访问组件 (MDAC) 2000.086.4360.00 (srv03_sp2_gdr.080822-1220) Microsoft MSXML 2.6 3.0 4.0 5.0 6.0 Microsoft Internet Explorer 7.0.5730.11 Microsoft .NET Framework 2.0.50727.3082 操作系统 5.2.3790
原来的代码在我的机器上: 5分30秒修改一下后31Sdeclare @t int set @T = 1000 --计算1000的阶乘 SET NOCOUNT ON declare @ta table(id int identity(1,1) primary key,col bigint) insert @ta(col) select 1declare @n int,@i int,@c bigint,@len int,@tmp bigintset @len=1; set @n = @t while (@n >=1) begin select @c = 0 ,@i = 1 select @tmp = col from @ta where id = @i while (@i <= @len) begin set @tmp = @tmp*@n+@c set @c = @tmp / 1000000000000000 set @tmp = @tmp % 1000000000000000 update @ta set col = @tmp where id = @i;
set @i = @i + 1 if not exists(select 1 from @ta where id = @i) insert @ta (col) select 0 select @tmp = col from @ta where id = @i end update @ta set col = @c where id = @i; if (@c>0) set @len = @len + 1 set @n = @n - 1 end delete from @ta where id >@len and col = 0 declare @s varchar(8000)select @s = isnull(@s+case when id%6 = 0 then char(13)+char(10) else ' ' end,'') +right('000000000000000'+ltrim(col),15) from @ta order by id desc select @sSET NOCOUNT Off/*-------------------------------------------------------------------------- 000000000000402 387260077093773 543702433923003 985719374864210 714632543799910 429938512398629 020592044208486 969404800479988 610197196058631 666872994808558 901323829669944 590997424504087 073759918823627 727188732519779 505950995276120 874975462497043 601418278094646 496291056393887 437886487337119 181045825783647 849977012476632 889835955735432 513185323958463 075557409114262 417474349347553 428646576611667 797396668820291 207379143853719 588249808126867 838374559731746 136085379534524 221586593201928 090878297308431 392844403281231 558611036976801 357304216168747 609675871348312 025478589320767 169132448426236 131412508780208 000261683151027 341827977704784 635868170164365 024153691398281 264810213092761 244896359928705 114964975419909 342221566832572 080821333186116 811553615836546 984046708975602 900950537616475 847728421889679 646244945160765 353408198901385 442487984959953 319101723355556 602139450399736 280750137837615 307127761926849 034352625200015 888535147331611 702103968175921 510907788019393 178114194545257 223865541461062 892187960223838 971476088506276 862967146674697 562911234082439 208160153780889 893964518263243 671616762179168 909779911903754 031274622289988 005195444414282 012187361745992 642956581746628 302955570299024 324153181617210 465832036786906 117260158783520 751516284225540 265170483304226 143974286933061 690897968482590 125458327168226 458066526769958 652682272807075 781391858178889 652208164348344 825993266043367 660176999612831 860788386150279 465955131156552 036093988180612 138558600301435 694527224206344 631797460594682 573103790084024 432438465657245 014402821885252 470935190620929 023136493273497 565513958720559 654228749774011 413346962715422 845862377387538 230483865688976 461927383814900 140767310446640 259899490222221 765904339901886 018566526485061 799702356193897 017860040811889 729918311021171 229845901641921 068884387121855 646124960798722 908519296819372 388642614839657 382291123125024 186649353143970 137428531926649 875337218940694 281434118520158 014123344828015 051399694290153 483077644569099 073152433278288 269864602789864 321139083506217 095002597389863 554277196742822 248757586765752 344220207573630 569498825087968 928162753848863 396909959826280 956121450994871 701244516461260 379029309120889 086942028510640 182154399457156 805941872748998 094254742173582 401063677404595 741785160829230 135358081840096 996372524230560 855903700624271 243416909004153 690105933983835 777939410970027 753472000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 */
stone,顺便也帮我把前面多余的0也去了.
看来还是sql处理数据牛啊,java需要1分多,sql只要不到10S
create table tb(id int IDENTITY(1,1) PRIMARY KEY,factor bigint,charfactor varchar(15)) insert into tb(factor) values(1) go SET NOCOUNT ON; declare @i int,@factormax int set @i=2 while @i<=1000 begin update tb set factor=factor*@i update tb set factor=factor+isnull((select floor(factor/1000000000000000) from tb where id=a.id-1),0) from tb a select @factormax=floor(factor/1000000000000000) from tb where id=(select top 1 id from tb order by id desc) if @factormax >0 insert into tb(factor) values(@factormax) update tb set factor=factor % 1000000000000000 set @i=@i+1 end update tb set charfactor=right('000000000000000'+convert(varchar,factor),15) update tb set charfactor=right(' '+convert(varchar,factor),15) from tb where id=(select top 1 id from tb order by id desc) select a.charfactor,isnull(b.charfactor,''),isnull(c.charfactor,''),isnull(d.charfactor,''),isnull(e.charfactor,''),isnull(f.charfactor,'') from tb a left join tb b on a.id=b.id+1 left join tb c on a.id=c.id+2 left join tb d on a.id=d.id+3 left join tb e on a.id=e.id+4 left join tb f on a.id=f.id+5 where a.id % 6=4 order by a.id desc go drop table tb /* charfactor --------------- --------------- --------------- --------------- --------------- --------------- 402 387260077093773 543702433923003 985719374864210 714632543799910 429938512398629 020592044208486 969404800479988 610197196058631 666872994808558 901323829669944 590997424504087 073759918823627 727188732519779 505950995276120 874975462497043 601418278094646 496291056393887 437886487337119 181045825783647 849977012476632 889835955735432 513185323958463 075557409114262 417474349347553 428646576611667 797396668820291 207379143853719 588249808126867 838374559731746 136085379534524 221586593201928 090878297308431 392844403281231 558611036976801 357304216168747 609675871348312 025478589320767 169132448426236 131412508780208 000261683151027 341827977704784 635868170164365 024153691398281 264810213092761 244896359928705 114964975419909 342221566832572 080821333186116 811553615836546 984046708975602 900950537616475 847728421889679 646244945160765 353408198901385 442487984959953 319101723355556 602139450399736 280750137837615 307127761926849 034352625200015 888535147331611 702103968175921 510907788019393 178114194545257 223865541461062 892187960223838 971476088506276 862967146674697 562911234082439 208160153780889 893964518263243 671616762179168 909779911903754 031274622289988 005195444414282 012187361745992 642956581746628 302955570299024 324153181617210 465832036786906 117260158783520 751516284225540 265170483304226 143974286933061 690897968482590 125458327168226 458066526769958 652682272807075 781391858178889 652208164348344 825993266043367 660176999612831 860788386150279 465955131156552 036093988180612 138558600301435 694527224206344 631797460594682 573103790084024 432438465657245 014402821885252 470935190620929 023136493273497 565513958720559 654228749774011 413346962715422 845862377387538 230483865688976 461927383814900 140767310446640 259899490222221 765904339901886 018566526485061 799702356193897 017860040811889 729918311021171 229845901641921 068884387121855 646124960798722 908519296819372 388642614839657 382291123125024 186649353143970 137428531926649 875337218940694 281434118520158 014123344828015 051399694290153 483077644569099 073152433278288 269864602789864 321139083506217 095002597389863 554277196742822 248757586765752 344220207573630 569498825087968 928162753848863 396909959826280 956121450994871 701244516461260 379029309120889 086942028510640 182154399457156 805941872748998 094254742173582 401063677404595 741785160829230 135358081840096 996372524230560 855903700624271 243416909004153 690105933983835 777939410970027 753472000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 */
create table tb(id int IDENTITY(1,1) PRIMARY KEY,factor bigint) insert into tb(factor) values(1) go declare @i int,@factormax int set @i=2 while @i<=1000 begin update tb set factor=factor*@i update tb set factor=factor+isnull((select floor(factor/1000000000000000) from tb where id=a.id-1),0) from tb a select @factormax=floor(factor/1000000000000000) from tb where factor>=1000000000000000 and id=(select top 1 id from tb order by id desc) if @factormax is not null insert into tb(factor) values(@factormax) update tb set factor=factor % 1000000000000000 set @i=@i+1 set @factormax=null end select right('000000000000000'+convert(varchar,factor),15) from tb order by id desc
呵呵!我用VB2005 写的 1秒不到 Dim int_factorial As Integer = TextBox1.Text '阶乘数 Dim a_array(3000) As Integer '存储阶乘数值数组 Dim a_int As Integer '数组下标 Dim int_for As Integer '阶乘数 Dim a_carry As Integer '进位数值 Dim a_remainder As Integer '余数 For a_int = 0 To 2999 '初始化数组 a_array(a_int) = 0 Next For int_for = 0 To int_factorial '算法循环次数 a_carry = 0 If int_for = 0 Then '如果阶乘数为0,则 a_array(0)=1 a_array(0) = 1 Else For a_int = 0 To 2999 '阶乘数不为0,则阶乘数分别乘以数组每一个元素 a_remainder = (a_array(a_int) * int_for + a_carry) Mod 10 '除以10取余数 a_carry = (a_array(a_int) * int_for + a_carry) \ 10 '除以10取进位数 a_array(a_int) = a_remainder '余数放回本元素,作为新值,进位数进位下一个元素 Next End If Next For a_int = 2999 To 0 Step -1 '把前面所有为0的元素去掉 If a_array(a_int) <> 0 Then Exit For End If Next Dim str As String = "" '由于数值太大,没有任何数据类型能存放,所以作为字符串存放 While a_int >= 0 str = str & a_array(a_int) a_int = a_int - 1 End While TextBox2.Text = str '输出数值 1000 阶乘计算结果: 402387260077093773543702433923003985719374864210714632543799910429 938512398629020592044208486969404800479988610197196058631666872994 808558901323829669944590997424504087073759918823627727188732519779 505950995276120874975462497043601418278094646496291056393887437886 487337119181045825783647849977012476632889835955735432513185323958 463075557409114262417474349347553428646576611667797396668820291207 379143853719588249808126867838374559731746136085379534524221586593 201928090878297308431392844403281231558611036976801357304216168747 609675871348312025478589320767169132448426236131412508780208000261 683151027341827977704784635868170164365024153691398281264810213092 761244896359928705114964975419909342221566832572080821333186116811 553615836546984046708975602900950537616475847728421889679646244945 160765353408198901385442487984959953319101723355556602139450399736 280750137837615307127761926849034352625200015888535147331611702103 968175921510907788019393178114194545257223865541461062892187960223 838971476088506276862967146674697562911234082439208160153780889893 964518263243671616762179168909779911903754031274622289988005195444 414282012187361745992642956581746628302955570299024324153181617210 465832036786906117260158783520751516284225540265170483304226143974 286933061690897968482590125458327168226458066526769958652682272807 075781391858178889652208164348344825993266043367660176999612831860 788386150279465955131156552036093988180612138558600301435694527224 206344631797460594682573103790084024432438465657245014402821885252 470935190620929023136493273497565513958720559654228749774011413346 962715422845862377387538230483865688976461927383814900140767310446 640259899490222221765904339901886018566526485061799702356193897017 860040811889729918311021171229845901641921068884387121855646124960 798722908519296819372388642614839657382291123125024186649353143970 137428531926649875337218940694281434118520158014123344828015051399 694290153483077644569099073152433278288269864602789864321139083506 217095002597389863554277196742822248757586765752344220207573630569 498825087968928162753848863396909959826280956121450994871701244516 461260379029309120889086942028510640182154399457156805941872748998 094254742173582401063677404595741785160829230135358081840096996372 524230560855903700624271243416909004153690105933983835777939410970 027753472000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000
如果是在PERL里就简单了 #!/usr/bin/perl -w use strict; use bigint; print 2**1000;
declare @i int
declare @j int
set @j=1
set @i=1
while(@i<=10)
begin
set @j=@i*@j
set @i=@i+1
end
print @j
int肯定不行了,没有字符串都表达不了结果。
这是今天在社区主页上看到JAVA算法的贴子
set @T = 10 --计算10的阶乘
SET NOCOUNT ON
declare @ta table(id int identity(1,1),col bigint)insert @ta(col) select 1
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
insert @ta(col) select 0
declare @n int,@i int,@c bigint,@len int,@tmp bigint,@tp bigintset @len=1;
set @n = @t
while (@n >=1)
begin
select @c = 0 ,@i = 1
select @tmp = col from @ta where id = @i
while (@i <= @len)
begin
set @tmp = @tmp*@n+@c
set @tp = @tmp
set @tmp = @tmp % 10000;
update @ta
set col = @tmp where id = @i;
set @c = @tp / 10000
set @i = @i + 1
select @tmp = col from @ta where id = @i
end
update @ta
set col = @c where id = @i;
if (@c>0)
set @len = @len + 1
set @n = @n - 1
end select * from @taSET NOCOUNT Off有人请客吃饭,来回来再把@ta的初始行数计算出来就可以1000
set @T = 40 --计算40的阶乘
SET NOCOUNT ON
declare @ta table(id int identity(1,1),col bigint)
insert @ta(col) select 1declare @n int,@i int,@c bigint,@len int,@tmp bigint,@tp bigintset @len=1;
set @n = @t
while (@n >=1)
begin
select @c = 0 ,@i = 1
select @tmp = col from @ta where id = @i
while (@i <= @len)
begin
set @tmp = @tmp*@n+@c
set @tp = @tmp
set @tmp = @tmp % 10000;
update @ta
set col = @tmp where id = @i;
set @c = @tp / 10000
set @i = @i + 1
if not exists(select 1 from @ta where id = @i)
insert @ta (col) select 0
select @tmp = col from @ta where id = @i
end
update @ta
set col = @c where id = @i;
if (@c>0)
set @len = @len + 1
set @n = @n - 1
end declare @s varchar(8000)
select @s = isnull(@s+' ','')+right('0000'+ltrim(col),4)
from @ta
order by id desc
select @sSET NOCOUNT Off/*
----------------------------------------------------------------
0000 8159 1528 3247 8977 3434 5611 2695 9611 5894 2720 0000 0000
*/对@ta自行判断要不生成行哪个测试一下1000 ,
嘿嘿,吃饭去了
create table tb(id int IDENTITY(1,1) PRIMARY KEY,factor bigint)
insert into tb(factor) values(1)
go
declare @i int,@factormax int
set @i=2
while @i<=1000
begin
update tb set factor=factor*@i
update tb set factor=factor+isnull((select floor(factor/1000000000000000) from tb where id=a.id-1),0) from tb a
select @factormax=floor(factor/1000000000000000) from tb where factor>=1000000000000000 and id=(select top 1 id from tb order by id desc)
if @factormax is not null
insert into tb(factor) values(@factormax)
update tb set factor=factor % 1000000000000000
set @i=@i+1
set @factormax=null
end
select right('000000000000000'+convert(varchar,factor),15) from tb order by id desc
go
drop table tb
运行时间:5秒结果大家自己看.
9000的阶乘,用时7分01秒.
create table tb(id int IDENTITY(1,1) PRIMARY KEY,factor bigint)
insert into tb(factor) values(1)
go
SET NOCOUNT ON;
declare @i int,@factormax int
set @i=2
while @i<=1000
begin
update tb set factor=factor*@i
update tb set factor=factor+isnull((select floor(factor/1000000000000000) from tb where id=a.id-1),0) from tb a
select @factormax=floor(factor/1000000000000000) from tb where id=(select top 1 id from tb order by id desc)
if @factormax >0
insert into tb(factor) values(@factormax)
update tb set factor=factor % 1000000000000000
set @i=@i+1
end
select right('000000000000000'+convert(varchar,factor),15) from tb order by id desc
go
drop table tb
1000的阶乘,运行时间2秒.
酷睿2双核E4300 1.8G, 2G DDRII 内存, 160G硬盘, 256M G7300GT显卡.
Microsoft Analysis Services 客户端工具 2005.090.3042.00
Microsoft 数据访问组件 (MDAC) 2000.086.4360.00 (srv03_sp2_gdr.080822-1220)
Microsoft MSXML 2.6 3.0 4.0 5.0 6.0
Microsoft Internet Explorer 7.0.5730.11
Microsoft .NET Framework 2.0.50727.3082
操作系统 5.2.3790
5分30秒修改一下后31Sdeclare @t int
set @T = 1000 --计算1000的阶乘
SET NOCOUNT ON
declare @ta table(id int identity(1,1) primary key,col bigint)
insert @ta(col) select 1declare @n int,@i int,@c bigint,@len int,@tmp bigintset @len=1;
set @n = @t
while (@n >=1)
begin
select @c = 0 ,@i = 1
select @tmp = col from @ta where id = @i
while (@i <= @len)
begin
set @tmp = @tmp*@n+@c
set @c = @tmp / 1000000000000000
set @tmp = @tmp % 1000000000000000
update @ta
set col = @tmp where id = @i;
set @i = @i + 1
if not exists(select 1 from @ta where id = @i)
insert @ta (col) select 0
select @tmp = col from @ta where id = @i
end
update @ta
set col = @c where id = @i;
if (@c>0)
set @len = @len + 1
set @n = @n - 1
end
delete from @ta where id >@len and col = 0
declare @s varchar(8000)select @s =
isnull(@s+case when id%6 = 0 then char(13)+char(10) else ' ' end,'')
+right('000000000000000'+ltrim(col),15)
from @ta
order by id desc
select @sSET NOCOUNT Off/*--------------------------------------------------------------------------
000000000000402 387260077093773 543702433923003 985719374864210
714632543799910 429938512398629 020592044208486 969404800479988 610197196058631 666872994808558
901323829669944 590997424504087 073759918823627 727188732519779 505950995276120 874975462497043
601418278094646 496291056393887 437886487337119 181045825783647 849977012476632 889835955735432
513185323958463 075557409114262 417474349347553 428646576611667 797396668820291 207379143853719
588249808126867 838374559731746 136085379534524 221586593201928 090878297308431 392844403281231
558611036976801 357304216168747 609675871348312 025478589320767 169132448426236 131412508780208
000261683151027 341827977704784 635868170164365 024153691398281 264810213092761 244896359928705
114964975419909 342221566832572 080821333186116 811553615836546 984046708975602 900950537616475
847728421889679 646244945160765 353408198901385 442487984959953 319101723355556 602139450399736
280750137837615 307127761926849 034352625200015 888535147331611 702103968175921 510907788019393
178114194545257 223865541461062 892187960223838 971476088506276 862967146674697 562911234082439
208160153780889 893964518263243 671616762179168 909779911903754 031274622289988 005195444414282
012187361745992 642956581746628 302955570299024 324153181617210 465832036786906 117260158783520
751516284225540 265170483304226 143974286933061 690897968482590 125458327168226 458066526769958
652682272807075 781391858178889 652208164348344 825993266043367 660176999612831 860788386150279
465955131156552 036093988180612 138558600301435 694527224206344 631797460594682 573103790084024
432438465657245 014402821885252 470935190620929 023136493273497 565513958720559 654228749774011
413346962715422 845862377387538 230483865688976 461927383814900 140767310446640 259899490222221
765904339901886 018566526485061 799702356193897 017860040811889 729918311021171 229845901641921
068884387121855 646124960798722 908519296819372 388642614839657 382291123125024 186649353143970
137428531926649 875337218940694 281434118520158 014123344828015 051399694290153 483077644569099
073152433278288 269864602789864 321139083506217 095002597389863 554277196742822 248757586765752
344220207573630 569498825087968 928162753848863 396909959826280 956121450994871 701244516461260
379029309120889 086942028510640 182154399457156 805941872748998 094254742173582 401063677404595
741785160829230 135358081840096 996372524230560 855903700624271 243416909004153 690105933983835
777939410970027 753472000000000 000000000000000 000000000000000 000000000000000 000000000000000
000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000
000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000
*/
insert into tb(factor) values(1)
go
SET NOCOUNT ON;
declare @i int,@factormax int
set @i=2
while @i<=1000
begin
update tb set factor=factor*@i
update tb set factor=factor+isnull((select floor(factor/1000000000000000) from tb where id=a.id-1),0) from tb a
select @factormax=floor(factor/1000000000000000) from tb where id=(select top 1 id from tb order by id desc)
if @factormax >0
insert into tb(factor) values(@factormax)
update tb set factor=factor % 1000000000000000
set @i=@i+1
end
update tb set charfactor=right('000000000000000'+convert(varchar,factor),15)
update tb set charfactor=right(' '+convert(varchar,factor),15) from tb where id=(select top 1 id from tb order by id desc)
select a.charfactor,isnull(b.charfactor,''),isnull(c.charfactor,''),isnull(d.charfactor,''),isnull(e.charfactor,''),isnull(f.charfactor,'')
from tb a left join tb b on a.id=b.id+1 left join tb c on a.id=c.id+2 left join tb d on a.id=d.id+3
left join tb e on a.id=e.id+4 left join tb f on a.id=f.id+5
where a.id % 6=4
order by a.id desc
go
drop table tb
/*
charfactor
--------------- --------------- --------------- --------------- --------------- ---------------
402 387260077093773 543702433923003 985719374864210 714632543799910 429938512398629
020592044208486 969404800479988 610197196058631 666872994808558 901323829669944 590997424504087
073759918823627 727188732519779 505950995276120 874975462497043 601418278094646 496291056393887
437886487337119 181045825783647 849977012476632 889835955735432 513185323958463 075557409114262
417474349347553 428646576611667 797396668820291 207379143853719 588249808126867 838374559731746
136085379534524 221586593201928 090878297308431 392844403281231 558611036976801 357304216168747
609675871348312 025478589320767 169132448426236 131412508780208 000261683151027 341827977704784
635868170164365 024153691398281 264810213092761 244896359928705 114964975419909 342221566832572
080821333186116 811553615836546 984046708975602 900950537616475 847728421889679 646244945160765
353408198901385 442487984959953 319101723355556 602139450399736 280750137837615 307127761926849
034352625200015 888535147331611 702103968175921 510907788019393 178114194545257 223865541461062
892187960223838 971476088506276 862967146674697 562911234082439 208160153780889 893964518263243
671616762179168 909779911903754 031274622289988 005195444414282 012187361745992 642956581746628
302955570299024 324153181617210 465832036786906 117260158783520 751516284225540 265170483304226
143974286933061 690897968482590 125458327168226 458066526769958 652682272807075 781391858178889
652208164348344 825993266043367 660176999612831 860788386150279 465955131156552 036093988180612
138558600301435 694527224206344 631797460594682 573103790084024 432438465657245 014402821885252
470935190620929 023136493273497 565513958720559 654228749774011 413346962715422 845862377387538
230483865688976 461927383814900 140767310446640 259899490222221 765904339901886 018566526485061
799702356193897 017860040811889 729918311021171 229845901641921 068884387121855 646124960798722
908519296819372 388642614839657 382291123125024 186649353143970 137428531926649 875337218940694
281434118520158 014123344828015 051399694290153 483077644569099 073152433278288 269864602789864
321139083506217 095002597389863 554277196742822 248757586765752 344220207573630 569498825087968
928162753848863 396909959826280 956121450994871 701244516461260 379029309120889 086942028510640
182154399457156 805941872748998 094254742173582 401063677404595 741785160829230 135358081840096
996372524230560 855903700624271 243416909004153 690105933983835 777939410970027 753472000000000
000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000
000000000000000 000000000000000 000000000000000 000000000000000 000000000000000 000000000000000
000000000000000 000000000000000 000000000000000 000000000000000 */
不过JAVA实现很简单,JAVA有大整数类可以直接用,别说1000!,就是(1000!)!也不成问题,
C++科学计数发实现较简单,精确计算较难,
SQL没用过
insert into tb(factor) values(1)
go
declare @i int,@factormax int
set @i=2
while @i<=1000
begin
update tb set factor=factor*@i
update tb set factor=factor+isnull((select floor(factor/1000000000000000) from tb where id=a.id-1),0) from tb a
select @factormax=floor(factor/1000000000000000) from tb where factor>=1000000000000000 and id=(select top 1 id from tb order by id desc)
if @factormax is not null
insert into tb(factor) values(@factormax)
update tb set factor=factor % 1000000000000000
set @i=@i+1
set @factormax=null
end
select right('000000000000000'+convert(varchar,factor),15) from tb order by id desc
要说不限制的应该是sql
操作数类型冲突: bigint 与 void type 不兼容
服务器: 消息 8117,级别 16,状态 1,行 11
操作数数据类型 numeric 无效(运算符 modulo)。
Dim a_array(3000) As Integer '存储阶乘数值数组
Dim a_int As Integer '数组下标
Dim int_for As Integer '阶乘数
Dim a_carry As Integer '进位数值
Dim a_remainder As Integer '余数
For a_int = 0 To 2999 '初始化数组
a_array(a_int) = 0
Next
For int_for = 0 To int_factorial '算法循环次数
a_carry = 0
If int_for = 0 Then '如果阶乘数为0,则 a_array(0)=1
a_array(0) = 1
Else
For a_int = 0 To 2999 '阶乘数不为0,则阶乘数分别乘以数组每一个元素
a_remainder = (a_array(a_int) * int_for + a_carry) Mod 10 '除以10取余数
a_carry = (a_array(a_int) * int_for + a_carry) \ 10 '除以10取进位数
a_array(a_int) = a_remainder '余数放回本元素,作为新值,进位数进位下一个元素
Next
End If
Next
For a_int = 2999 To 0 Step -1 '把前面所有为0的元素去掉
If a_array(a_int) <> 0 Then
Exit For
End If
Next
Dim str As String = "" '由于数值太大,没有任何数据类型能存放,所以作为字符串存放
While a_int >= 0
str = str & a_array(a_int)
a_int = a_int - 1
End While
TextBox2.Text = str '输出数值
1000 阶乘计算结果:
402387260077093773543702433923003985719374864210714632543799910429
938512398629020592044208486969404800479988610197196058631666872994
808558901323829669944590997424504087073759918823627727188732519779
505950995276120874975462497043601418278094646496291056393887437886
487337119181045825783647849977012476632889835955735432513185323958
463075557409114262417474349347553428646576611667797396668820291207
379143853719588249808126867838374559731746136085379534524221586593
201928090878297308431392844403281231558611036976801357304216168747
609675871348312025478589320767169132448426236131412508780208000261
683151027341827977704784635868170164365024153691398281264810213092
761244896359928705114964975419909342221566832572080821333186116811
553615836546984046708975602900950537616475847728421889679646244945
160765353408198901385442487984959953319101723355556602139450399736
280750137837615307127761926849034352625200015888535147331611702103
968175921510907788019393178114194545257223865541461062892187960223
838971476088506276862967146674697562911234082439208160153780889893
964518263243671616762179168909779911903754031274622289988005195444
414282012187361745992642956581746628302955570299024324153181617210
465832036786906117260158783520751516284225540265170483304226143974
286933061690897968482590125458327168226458066526769958652682272807
075781391858178889652208164348344825993266043367660176999612831860
788386150279465955131156552036093988180612138558600301435694527224
206344631797460594682573103790084024432438465657245014402821885252
470935190620929023136493273497565513958720559654228749774011413346
962715422845862377387538230483865688976461927383814900140767310446
640259899490222221765904339901886018566526485061799702356193897017
860040811889729918311021171229845901641921068884387121855646124960
798722908519296819372388642614839657382291123125024186649353143970
137428531926649875337218940694281434118520158014123344828015051399
694290153483077644569099073152433278288269864602789864321139083506
217095002597389863554277196742822248757586765752344220207573630569
498825087968928162753848863396909959826280956121450994871701244516
461260379029309120889086942028510640182154399457156805941872748998
094254742173582401063677404595741785160829230135358081840096996372
524230560855903700624271243416909004153690105933983835777939410970
027753472000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000
#!/usr/bin/perl -w
use strict;
use bigint;
print 2**1000;
回家也写个sql计算下