# 用MSSQL计算2的10000次方

----------本机测试 1秒---------

SET NOCOUNT ON

GO

IF OBJECT_ID('BASED') IS NOT NULL DROP TABLE BASED

IF OBJECT_ID('TB') IS NOT NULL DROP TABLE TB

GO

CREATE 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

)T

insert 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

/*

199506311688075838488374216268358508382349683188619245

.....

*/

---------------本机22秒

A的B次方

create FUNCTION [dbo].[powerX](@loopnumber bigint,@loopcount int)

RETURNS varchar(max)

AS

BEGIN

declare @str varchar(max)

declare @loop int

declare @len int

declare @num bigint

declare @output varchar(max)

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

set @loop=1

while (@loop<@loopcount)

begin

set @output=''

set @len=len(@str)

while (@len!=0)

begin

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

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

set @len=@len-1

end

begin

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

end

set @loop=@loop+1

set @str=@output

end

return @str

end

declare @val bigint

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

select dbo.powerX(@val,400)

-------------------

http://topic.csdn.net/u/20110108/18/c4d2e6e0-ee57-4f6b-84e4-51dab792fa1f.html

#### 计算4的10000次方（C#）

2008年04月28日 18KB 下载

#### 计算2^1000/2^10000的各位数和

2014-09-15 20:22:01

#### java 大数据次方运算

2013-02-11 19:46:57

#### C/C++ 编程计算2的100万次方（m的n次方）,超长结果输出文件

2016-03-25 10:30:41

#### 用数组计算2的N次方

2016-01-28 17:09:21

#### 汇编语言：使用汇编程序计算2的8次方

2018-01-22 14:38:08

#### How:汇编语言计算2的n次方的一个实例

2012-03-14 16:07:13

#### c语言实现输出2的1000次方

2016-09-02 01:22:38

#### 2的N次方的&和|运算

2018-03-14 23:29:22

#### Python计算2的38次幂

2014-10-23 20:16:32