您的位置:首页 > 其它

计算1000的阶乘

2009-04-12 18:21 399 查看
------------------------------------------------------------------------
-- Author: happyflystone
-- Date : 2009-04-12 18:22:00
-- Ver: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86)
-- Apr 14 2006 01:12:25
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Standard Edition on Windows NT 5.0 (Build 2195: Service Pack 4)
--
------------------------------------------------------------------------




declare @t int
set @T = 1000 --计算的阶乘
SET NOCOUNT ON
declare @ta table(id int identity(1,1) primary key,col bigint)
insert @ta(col) select 1



declare @n int,@i int,@c bigint,@len int,@tmp bigint

set @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)
--显示串,5个一组
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 @s

SET 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
*/


----优化一次:2009-04-13
declare @t int,@c int ,@i int
set @I = 1000 --计算的阶乘
set @T = 1
SET NOCOUNT ON
declare @ta table(id int identity(1,1) primary key,col bigint)
insert @ta(col) select 1
while @T<=@I
begin
update @ta
set col=col*@T +isnull((select (col*@T/1000000000000000)
from @ta where id=a.id-1),0) ,
@c=(col*@T/1000000000000000)
from @ta a
if @c >0
insert into @ta(col) values(@c)
update @ta set col=col % 1000000000000000
set @T=@T+1
end
declare @s varchar(8000)
--显示串,个一组
select @s =
isnull(@s+case when id%5 = 0 then char(13)+char(10) else ' ' end,'')
+right('000000000000000'+ltrim(col),15)
from @ta
order by id desc

select @s
go


2009-4-15:谢谢的chenyonghui1988 提醒,在SQL2000下要强制类型转换:

declare @t int,@c int ,@i int
set @I = 1000 --计算1000的阶乘
set @T = 1
SET NOCOUNT ON
declare @ta table(id int identity(1,1) primary key,col bigint)
insert @ta(col) select 1
while @T<=@I
begin
update @ta
set col=col*@T +isnull((select (col*@T/1000000000000000)
from @ta where id=a.id-1),0) ,
@c=(col*@T/1000000000000000)
from @ta a
if @c >0
insert into @ta(col) values(@c)
update @ta set col=col % cast(1000000000000000 as bigint)
set @T=@T+1
end
declare @s varchar(8000)
--显示串,6个一组
select @s =
isnull(@s+case when id%5 = 0 then char(13)+char(10) else ' ' end,'')
+right('000000000000000'+ltrim(col),15)
from @ta
order by id desc
select @s
go
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: