用MSSQL計算2的10000次方

2021-10-22 23:31:49 字數 3330 閱讀 6184

在論壇看到 牛人們的 思路 特此收藏啊 以備學習

----------本機測試 1秒---------

用decimal(38,0),0秒:

set nocount on

goif object_id('based') is not null drop table based

if object_id('tb') is not null drop table tb

gocreate table based(id int primary key,val bigint)

insert into based select 0,1 union all

select id,power(convert(decimal(38,0),2),id) from(

select number as id from master..spt_values where type='p' and number>0 and number<57

)tinsert into based select number+57,power(convert(decimal(38,0),2),56)*power(2,number+1) from master..spt_values where type='p' and number<4

--以上求出2^1~2^60,因power函式在2^56以上時有捨去位,故分兩步處理,以乘法代之

declare @i int ,@count int,@result varchar(max),@p60 decimal(38,0),@pleft decimal(38,0)

select @i=1,@result='',@count=10000 --@count=10000就是計算2的10000次方

select @p60=val from based where id=60

select @pleft=val from based where id=@count % 60

create table tb(id int primary key,val decimal(38,0))

insert into tb select 1,1

insert into tb

select number,0 from(

select number from master..spt_values where type='p' and number>1 and number<=@count/60+2

)t--設總行數為 @count/60+2

while @i<=@count/60

begin

update t1 set t1.val= (t1.val*@p60+floor((isnull(t2.val,0)*@p60+floor(isnull(t3.val,0)*@p60/10000000000000000000))/10000000000000000000))%10000000000000000000

from tb t1 left join tb t2 on t1.id=t2.id+1 left join tb t3 on t2.id=t3.id+1

set @i=@i+1

end-- @count/60 次 @p60 相乘

update t1 set t1.val= (t1.val*@pleft+floor((isnull(t2.val,0)*@pleft+floor(isnull(t3.val,0)*@pleft/10000000000000000000))/10000000000000000000))%10000000000000000000

from tb t1 left join tb t2 on t1.id=t2.id+1 left join tb t3 on t2.id=t3.id+1

--乘以剩下的 2^(@count % 60)

select @result=@result+right('000000000000'+convert(varchar(19),val),19) from tb

where val>0

order by id desc

--select @result=convert(varchar(10),val)+@result from tb

--where and id=@count/30+2

select right(@result,len(@result)-patindex('%[1-9]%',@result)+1)

set nocount off

每次運算都*2^30,最後一次補足10000,迴圈數量變為原來的1/30了,而且每次的運算都是由系統進行,比進製運算要快很多,肯定是順間出來的。

---------------本機22秒

a的b次方

create function [dbo].[powerx](@loopnumber bigint,@loopcount int)

returns varchar(max)

asbegin

declare @str varchar(max)

declare @loop int

declare @len int

declare @num bigint

declare @add int

declare @output varchar(max)

set @str=cast(@loopnumber as varchar(max))

set @loop=1

while (@loop<@loopcount)

begin

set @output=''

set @add=0

set @len=len(@str)

while (@len!=0)

begin

set @num = cast(substring(@str,@len,1) as int)

set @num=@num*@loopnumber+@add

set @add=@num/10

set @output=cast(@num%10 as varchar(max)) +@output

set @len=@len-1

endif(@add>=1)

begin

set @output = cast(@add as varchar(max))+@output

endset @loop=@loop+1

set @str=@output

endreturn @str

enddeclare @val bigint

select @val=cast(dbo.powerx(2,25)as bigint)

select dbo.powerx(@val,400)

詳情 見論壇 :

用MSSQL計算2的10000次方

在論壇看到 牛人們的 思路 特此收藏啊 以備學習 本機測試 1秒 用decimal 38,0 0秒 set nocount on goif object id based is not null drop table based if object id tb is not null drop ta...

用MSSQL計算2的10000次方

在論壇看到 牛人們的 思路 特此收藏啊 以備學習 本機測試 1秒 用decimal 38,0 0秒 set nocount on goif object id based is not null drop table based if object id tb is not null drop ta...

分組計算資料的合MSSQL

一張單位表unit id name 1 中石油 2 中移動 3 電信 另一張表info id unitid num point 1 1 1 2 2 1 2 1 3 2 1 1 4 3 2 2 若num 1時 得1分 若num 2時 得3分 若point 1時 得1分 若point 2 得3分 還有在...