VB编程,求代码 输出[100.200]之间所有素数

要求:采用Function过程,输出区间内所有素数以及输出“[100.200]之间共有素数个数...个”,每行输出7个数据。

素数.只能被1和他自己本身整除..整除使用mod函数判断

Dim i As Integer
Dim a As Integer
Dim b As Integer
Dim mb As Boolean
Dim msStr As String

b = 1
For i = 100 To 200
mb = True
For a = 1 To i
If (i Mod a) = 0 Then
If a <> 1 And a <> i Then
mb = False
Exit For
End If
End If
Next a

If mb = True Then
If b <= 7 Then
msStr = msStr & " " & i
b = b + 1
Else
b = 1
msStr = msStr & vbCrLf & " " & i
b = b + 1
End If
End If
Next i

MsgBox msStr
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2016-10-23
Private Sub Command1_Click()
Dim i As Integer, s As Integer
For i = 100 To 200
If f1(i) Then
Print i;
s = s + 1
If s Mod 7 = 0 Then Print
End If
Next
Print
Print "个数:";
Print s
End Sub
Public Function f1(x As Integer) As Boolean
Dim n As Integer
n = 1
Do While n < x
n = n + 1
If x Mod n = 0 Then
f1 = False
Exit Do
End If
Loop
If n >= x Then f1 = True
End Function

本回答被提问者采纳
第2个回答  2015-06-16
Public Function chackSUSU(a As Integer) As Boolean
Dim i As Integer
For i = 2 To a ^ 0.5
If a Mod i = 0 Then chackSUSU = False: Exit Function
Next
chackSUSU = True
End Function

Sub test()
Dim i As Integer
For i = 100 To 200
If chackSUSU(i) Then Debug.Print i
Next
End Sub
相似回答