用VBS精确计算2的100次方的代码
程序员文章站
2022-08-27 15:40:10
既然python可以计算2的100次方,那么我就要用vbs实现。不过这个效率嘛,计算2的10000次方python用了0.009013秒,vbs用了120.9805秒,不是...
既然python可以计算2的100次方,那么我就要用vbs实现。不过这个效率嘛,计算2的10000次方python用了0.009013秒,vbs用了120.9805秒,不是一个等级的,我就不多说什么了。
直接上代码:
'date: 2010/10/27
'author: demon
'qq: 380401911
'e-mail: still.demon@gmail.com
begin = timer
n = 1
for i = 1 to 100
n = multiple(n, 2)
next
finish = timer
wscript.echo n
wscript.echo finish - begin
'grade school multiplication, algorithm 14.12
'http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
function multiple(byval x, byval y)
dim n, t, i, j, z, w()
n = len(x) - 1
t = len(y) - 1
redim w(n + t + 1)
x = cstr(x) : y = cstr(y)
for i = 0 to ubound(w)
w(i) = "0"
next
for i = 0 to t
dim c : c = 0
dim uv : uv = 0
for j = 0 to n
uv = (w(i+j)-"0") + c + _
(mid(x,n-j+1,1)-"0") * (mid(y,t-i+1,1)-"0")
w(i+j) = cstr(uv mod 10 + "0")
c = uv \ 10
next
w(i+n+1) = cstr(uv \ 10 + "0")
next
z = join(w,"")
z = strreverse(z)
do while left(z,1) = "0"
z = mid(z,2)
loop
multiple = z
end function
原文:http://demon.tw/programming/vbs-long-multiplication.html
直接上代码:
复制代码 代码如下:
'date: 2010/10/27
'author: demon
'qq: 380401911
'e-mail: still.demon@gmail.com
begin = timer
n = 1
for i = 1 to 100
n = multiple(n, 2)
next
finish = timer
wscript.echo n
wscript.echo finish - begin
'grade school multiplication, algorithm 14.12
'http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf
function multiple(byval x, byval y)
dim n, t, i, j, z, w()
n = len(x) - 1
t = len(y) - 1
redim w(n + t + 1)
x = cstr(x) : y = cstr(y)
for i = 0 to ubound(w)
w(i) = "0"
next
for i = 0 to t
dim c : c = 0
dim uv : uv = 0
for j = 0 to n
uv = (w(i+j)-"0") + c + _
(mid(x,n-j+1,1)-"0") * (mid(y,t-i+1,1)-"0")
w(i+j) = cstr(uv mod 10 + "0")
c = uv \ 10
next
w(i+n+1) = cstr(uv \ 10 + "0")
next
z = join(w,"")
z = strreverse(z)
do while left(z,1) = "0"
z = mid(z,2)
loop
multiple = z
end function
原文:http://demon.tw/programming/vbs-long-multiplication.html
上一篇: 在VBS中定义字节数组Byte()介绍
下一篇: 张都监为什么要骗武松?原因是什么