vb编程求2到200之内所有素数的和。用独立函数判断某个数是否为素数。

2024-12-17 10:51:33
推荐回答(2个)
回答1:

Private Sub Form_Click()
For i = 2 To 3
s = 2
For j = 2 To i - 1
If i Mod j = 0 Then
Else
s = s + i
End If
Next j
Next i
Print "素数和为" & s
End Sub

Private Sub Command1_Click() '按下
’Command1判断任意数是否被素数
x = InputBox("输入一个数")
If x = 1 Or x = 2 Then
MsgBox (x & "为素数")
Else
For k = 2 To x - 1
If x Mod k = 0 Then
MsgBox (x & "不为素数")
End
Else
If k = x - 1 Then
MsgBox (x & "为素数")
End If
End If
Next k
End If
End Sub

回答2:

for i=2 to 200
if f(i) then s=s+i
print i

function f(a)
f=0
for i=2 to a
if a mod i=0 then exit function
next i
f=1
end function