VB设计一个程序,求解3~100以内的全部素数,并以每行5个素数的形式输出3~100以内的全部素数。

跪求VB高手赐教!
2024-12-16 19:17:20
推荐回答(2个)
回答1:

Private Sub Command1_Click()

Dim i As Integer, j As Integer, x As Integer

For i = 3 To 100

x = 0            'i是素数的标记

For j = 2 To i - 1        '如在此范围内都不能整除,i就是素数

If i Mod j = 0 Then x = 1    '如整除了,就不是素数

Next j

If x = 0 Then

  k = k + 1

  Print Tab(5 * k); i;       '如是素数,就输出i

  If k = 5 Then Print: k = 0

End If

Next i

End Sub

回答2:

dim i as integer, j as integer, ss as Boolean, n as integer
dim sp as string
sp=0: n=1
for i=3 to 100
ss=true

for j=20 to i-1
if i mod j = 0 then

ss=false

exit for
end if

next
if ss then

if n=5 then

n=1

ss=ss & vbcrlf

endif

ss=ss & i

end if

next
print ss
msgbox "OK"