主题:阶乘
moz
[专家分:37620] 发布于 2007-05-24 21:15:00
DECLARE FUNCTION js$ (n&)
DECLARE FUNCTION xxx$ (s$, i&)
DEFLNG A-Z
t1# = TIMER
PRINT js$(1000)
PRINT TIMER - t1#
FUNCTION js$ (n)
s$ = "1"
FOR i = 1 TO n
s$ = xxx$(s$, (i))
DO WHILE RIGHT$(s$, 1) = "0"
k = k + 1
s$ = LEFT$(s$, LEN(s$) - 1)
LOOP
NEXT
js$ = s$ + STRING$(k, "0")
END FUNCTION
FUNCTION xxx$ (s$, i)
L3 = 8 - LEN(STR$(i))
p = 10 ^ L3
v = 0
FOR j = (LEN(s$) - L3 + 1) TO 1 STEP -L3
v = VAL(MID$(s$, j, L3)) * i + v
ss$ = RIGHT$(STR$(v), L3) + ss$
v = v \ p
NEXT
IF j > 1 - L3 THEN v = VAL(LEFT$(s$, j + L3 - 1)) * i + v
IF v > 0 THEN ss$ = STR$(v) + ss$
xxx$ = LTRIM$(ss$)
END FUNCTION
回复列表 (共26个回复)
沙发
Matodied [专家分:7560] 发布于 2007-05-24 21:39:00
你的阶乘是怎么算的,那么快?我的虽然能算出正确结果,但是要10分钟左右才能出来。
CLS
INPUT n
PRINT n; "!=";
DIM a(n), b(n), s(n): l$ = "1"
FOR i = 1 TO n
ERASE a, b, s
a$ = l$: b$ = LTRIM$(STR$(i))
GOSUB gjdmult
l$ = ""
FOR j = ls TO 1 STEP -1
l$ = l$ + RTRIM$(LTRIM$(STR$(s(j))))
NEXT j
NEXT i
PRINT " "; l$: END
gjdmult: la = LEN(a$): lb = LEN(b$): DIM a(la), b(lb), s(la + lb)
FOR k = 1 TO la: a(k) = VAL(MID$(a$, la + 1 - k, 1)): NEXT k
FOR k = 1 TO lb: b(k) = VAL(MID$(b$, lb + 1 - k, 1)): NEXT k
FOR k = 1 TO la: FOR l = 1 TO lb
d = a(k) * b(l): v = k + l - 1
s(v) = s(v) + d MOD 10: s(v + 1) = s(v + 1) + s(v) \ 10 + d \ 10
s(v) = s(v) MOD 10
NEXT l, k
IF s(la + lb) = 0 THEN ls = la + lb - 1 ELSE ls = la + lb
RETURN
板凳
moz [专家分:37620] 发布于 2007-05-25 09:18:00
因为1000比较小,再来一次优化过程,时间再减半.
DEFLNG A-Z
t1# = TIMER
PRINT js2$(1000)
PRINT TIMER - t1#
FUNCTION js2$ (n)
s$ = "1"
FOR i = (n mod 2+1) TO n STEP 2
s$ = xxx$(s$, i * i + i)
DO WHILE RIGHT$(s$, 1) = "0"
k = k + 1
s$ = LEFT$(s$, LEN(s$) - 1)
LOOP
NEXT
js2$ = s$ + STRING$(k, "0")
END FUNCTION
FUNCTION xxx$ (s$, i)
L3 = 10 - LEN(STR$(i))
IF L3 < 1 THEN L3 = 1
p = 10 ^ L3
v = 0
FOR j = (LEN(s$) - L3 + 1) TO 1 STEP -L3
v = VAL(MID$(s$, j, L3)) * i + v
ss$ = RIGHT$(STR$(v), L3) + ss$
v = v \ p
NEXT
IF j > 1 - L3 THEN v = VAL(LEFT$(s$, j + L3 - 1)) * i + v
IF v > 0 THEN ss$ = STR$(v) + ss$
xxx$ = LTRIM$(ss$)
END FUNCTION
其实for的step还可以是3的,下面的式子是相等的,
1*2*3 * 4*5*6 * 7*8*9*....
(2*2*2-2)*(5*5*5-5)*(8*8*8-8)*....
(3*3*3-3)*(6*6*6-6)*(9*9*9-9)*.....
不过因为1000^3数字太长,不适宜整数运算,作罢.
3 楼
moz [专家分:37620] 发布于 2007-05-25 18:30:00
反正1000太小,再来:
DEFCUR A-Z
t1# = TIMER
m = 1
FOR i = 1 TO 16
m = m * i
NEXT
s$ = LTRIM$(STR$(m))
FOR i = 18 TO 999 STEP 3
DO WHILE RIGHT$(s$, 1) = "0"
k = k + 1
s$ = LEFT$(s$, LEN(s$) - 1)
LOOP
s$ = xxx$(s$, i * i * i - i))
NEXT
PRINT s$ + STRING$(k, "0")
PRINT TIMER - t1#
FUNCTION xxx$ (s$, i)
L3 = 15 - LEN(STR$(i))
IF L3 < 1 THEN L3 = 1
p = 10 ^ L3
v = 0
FOR j = (LEN(s$) - L3 + 1) TO 1 STEP -L3
v = VAL(MID$(s$, j, L3)) * i + v
ss$ = RIGHT$(STR$(v), L3) + ss$
v = FIX(v / p)
NEXT
IF j > 1 - L3 THEN v = VAL(LEFT$(s$, j + L3 - 1)) * i + v
IF v > 0 THEN ss$ = STR$(v) + ss$
xxx$ = LTRIM$(ss$)
END FUNCTION
4 楼
Matodied [专家分:7560] 发布于 2007-05-26 11:54:00
moz,有DEFCUR这个语句吗?
5 楼
moz [专家分:37620] 发布于 2007-05-26 13:45:00
在QB71里面是有的
你看看数据类型里面有多少种
精度最高的是货币型,有19位有效数字
defcur 把所有变量默认为货币型,
好像是current......什么的.
6 楼
Templar9d [专家分:2110] 发布于 2007-05-26 23:53:00
哦...7.1了.....
见识了~~~我说怎么4.5不能直接跑你的程序。
货币是currency吧~很少见人用,没想到精度这么高...
7 楼
Matodied [专家分:7560] 发布于 2007-05-29 14:42:00
moz,前几天的一篇叫“abcdefg”的帖子是不是你删掉了?怎么不见了?
8 楼
wangyunchaoPIG [专家分:130] 发布于 2007-06-03 15:35:00
看不懂??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
9 楼
a292982250 [专家分:40] 发布于 2007-06-13 13:35:00
给我打高分
10 楼
Lovely哆啦 [专家分:1360] 发布于 2007-07-21 14:04:00
请说一下思路
我来回复