vb 编写程序求出100-200间的所有素数

本人没有那么多财富了 只能尽我所能了

第1个回答  2011-06-29
Sub test()
Dim i As Integer
Dim j As Integer
Dim m As Boolean
For i = 100 To 200
m = True
For j = 2 To i ^ 0.5
If i Mod j = 0 Then
m = False
Exit For
End If
Next j
If m Then Print i
Next i
End Sub本回答被提问者采纳
第2个回答  2011-06-29
Public Sub iTest()
'求 x - y 之间的素数
Dim x, y, t As Boolean, i, j, k, res, msg$, n, tmp

x = 100 '最小值
y = 200 '最大值

For i = x To y
If i Mod 2 <> 0 Then
j = 3
k = Int(i ^ (0.5)) + 1
t = True
Do While j <= k
If i Mod j = 0 Then t = False: Exit Do
j = j + 2
Loop
If t Then res = res & " " & i
End If
Next
tmp = Split(Trim(res), " ")
n = UBound(tmp) + 1
msg = x & " - " & y & " 之间有 " & n & " 个素数:"
MsgBox msg & Chr(10) & Join(tmp, ",")
' Print msg & Chr(10) & Join(tmp, ",")
End Sub
第3个回答  2011-06-30
cls
s=0
for i=3 to 200 step 2
for j=2 to int(sqr(i)
if i mod j=0
s=s+i
exit for
endif
next j
next i
print "3到200之间的所有非偶数非素数的数之和:";s
end
相似回答