下面的算法在 http://www.csdn.net/develop/edit.asp?id=28308基础上改进,比其至少快10%:
【相关文章:在C#中操作XML】sub calcfactorial(byval n as integer) 【扩展阅读:XML数据库探讨1】
【扩展信息:调用Shell接口(系统运行窗口)的代码】dim xys() as integer, x() as integer, y() as integer, result() as string, i as long, j as long, k as long, temp as long, stimer as doubleif n < 0 then exit sub redim x(1) redim xys(1) x(1) = 1 xys(1) = 1 k = 1 stimer = timer do while k <= ntemp = len(cstr(k))
redim y(1 to temp) for i = 1 to temp y(i) = val(mid(k, temp + 1 - i, 1)) next redim xys(1 to ubound(x) + ubound(y)) for i = 1 to ubound(x) for j = 1 to ubound(y) xys(i + j - 1) = xys(i + j - 1) + x(i) * y(j) next next for i = 1 to ubound(x) + ubound(y) - 1 temp = xys(i) \ 10 xys(i) = xys(i) mod 10 xys(i + 1) = xys(i + 1) + temp nextx = xys
if x(ubound(x)) = 0 then redim preserve x(1 to ubound(x) - 1) k = k + 1 loop redim result(1 to ubound(x)) for i = 1 to ubound(x) result(i) = x(ubound(x) + 1 - i) nextfactorial = join(result, "")
debug.print k - 1 & "! : 用时 "; timer - stimer & " 秒, 结果 " & ubound(x) & " 位" ´debug.print factorial ... 下一页