vb编程,1000以内所有素数和,急用

最好详细点。。。急啊,就是do while的,不是for的
2024-10-30 10:22:58
推荐回答(2个)
回答1:

刚做了一道:
Private Sub command1_click()
Dim i, j, f, c,sum
i = 2
While i < 1000
f = True
j = 2
Do While j < i
If i Mod j = 0 Then f = False: Exit Do
j = j + 1
Loop
If f Then
debug.Print i & vbTab;
c = c + 1
sum=sum+i
If c Mod 5 = 0 Then debug.Print
End If
i = i + 1
Wend
print vbcrlf & "1000之内所有素数之和=" & sum
End Sub

回答2:

dim i,j as integer
dim sum as long
i=2
do while i<=1000
j=2
f=0
do while j<=i-1 and f=0
if i mod j = 0 then
f=1
end if
j=j+1
loop
if f=0 then
sum=sum+i
end if
i=i+1
loop