VB编程:求1000以内素数之和

2024-11-26 04:40:06
推荐回答(5个)
回答1:

Dim n, x, r, s As Single
n = 2
s = 0
Do While n <= 1000
r = 0
x = 2
Do While x <= n / 2
If n Mod x = 0 Then
r = 1
End If
x = x + 1
Loop
If r = 0 Then
s = s + n
End If
n = n + 1
Loop
Print s

调试后答案为76127

回答2:

我用手机打的,你最好运行下,
Dim
i
As
Integer:n
As
Integer
Do
While
i〈1000
i=1:x=0
s:
i=i+1
ll:
n=i-1:t=i
Mod
n
If
n=1
Then
Goto
s:x=x+i
If
t
〈〉0
Then
Goto
ll
If
t=0
Then
Goto
s
Loop
Print
x
我想我的代码会比上面的简单一些。希望能帮到你。不懂的可以追问。谢谢!

回答3:

Private Sub Form_Click()
Dim i, j, b As Long
For i = 1 To 1000
For j = 2 To i - 1

If i Mod j = 0 Then Exit For

Next

If j > i - 1 Then b = b + i

Next
Print b

End SuB

回答4:

Private Sub Command1_Click()
Dim i As Integer
s = 0
For i = 2 To 1000
If isNumss(i) Then s = s + i
Next i
Print s
End Sub

Function isNumss(n As Integer) As Long
For i = 2 To n - 1
If n Mod i = 0 Then
isNumss = 0
Exit Function
End If
Next
isNumss = 1
End Function

回答5:

dim a() as integer
a(0)=0
b=2
for i =3 to 1000
for j =0 to ubound (a())
if i\a(j)<>0 then
redim preserve a(ubound(a())+1)
b=b+i
end if
next j
next j
print b