是我的算法问题吗?
vbs吧
全部回复
仅看楼主
level 14
计算个1000000以内素数个数就要很久。
2023年11月14日 06点11分 1
level 14
m=inputbox("计算?以内的素数个数","素数个数","100")
gs=4 '从11开始计算,之前素数个数为4个
for i = 11 to m step 2 '偶数直接跳过,所以每次+2
f = int(sqr(i)) '计算不大于i的平方根的最大整数
ss = 1 '累加计数器,素数个数为1
for j = 3 to f step 2 '从3开始进行试除,同样跳过偶数
p = i mod j 'i除以j求余数
if p=0 then '余数为0,i为合数
ss = 0 '素数个数改为0
j=f '令被除数赋值循环最大数,跳出循环
end if
next
gs = gs +ss '素数个数进行累加
next
msgbox m & "以内的素数总个数为:" & gs & "个"
2023年11月14日 06点11分 2
代码如上
2023年11月14日 06点11分
level 14
和吧里学的,加了一个进度条,好用了。
2023年11月14日 08点11分 3
level 7
vbs就是慢的 算法没啥问题
2023年11月18日 15点11分 4
level 7
开几个internet explorer组件 用js“多线程计算”
手动狗头
2024年02月05日 02点02分 6
lz只会vbs
2024年02月05日 03点02分
level 7
你看看这个怎么样
function getprimefrom2(num)
dim a()
redim a(-1)
dim bool_
bool_=true
for i=2 to num
bool_=true
for j=0 to ubound(a)
if i mod a(j)=0 then
bool_=false
exit for
end if
next
if bool_ then
redim preserve a(ubound(a)+1)
a(ubound(a))=i
end if
next
msgbox ubound(a)+1
end function
getprimefrom2(10000)
还能优化不少其实
2024年02月05日 06点02分 7
level 7
这样似乎更快一点
function getprimefrom2(num)
dim a(),b
b=1
redim a(b)
a(0)=2
a(1)=3
dim bool_
bool_=true
for k=6 to num step 6
if k mod 10000 <=6 then wscript.stdout.writeline(k)
for i=k-1 to k+1 step 2
bool_=true
for j=0 to ubound(a)
if i mod a(j)=0 then
bool_=false
exit for
end if
next
if bool_ then
b=b+1
redim preserve a(b)
a(b)=i
end if
next
next
msgbox b+1
end function
getprimefrom2(1000000)
wscript.stdin.readline
2024年02月05日 06点02分 8
level 7
越来越慢是因为到后面要找的数组越来越长
2024年02月05日 06点02分 9
level 7
这也算埃氏吧
2024年02月05日 06点02分 10
level 7
function getprimefrom2(num)
set a=CreateObject("System.Collections.ArrayList")
a.add(2)
a.add(3)
dim bool_
bool_=true
for k=6 to num step 6
for i=k-1 to k+1 step 2
bool_=true
for j=0 to a.count-1
if i mod a.item(j)=0 then
bool_=false
exit for
end if
next
if bool_ then
a.add(i)
end if
next
next
msgbox a.count
end function
getprimefrom2(10000)
换了个数组[开心]
2024年02月05日 06点02分 11
1