vb编程。编写一个程序prime(m%),判断m是否为素数,若是,函数返回值为true,否则为false。多谢啦

然后调用该函数找出小于200的所有素数并显示。
2024-11-14 23:54:41
推荐回答(1个)
回答(1):

Private Sub Command1_Click()
Dim n%, i%
For n = 2 To 200
If prime(n) Then
Print n,
i = i + 1
If i Mod 4 = 0 Then Print
End If
Next n
End Sub
Private Function prime(m%) As Boolean
Dim i%, f As Boolean
f = True
For i = 2 To Sqr(m)
If m Mod i = 0 Then
f = False
Exit For
End If
Next i
prime = f
End Function