vb代码问题 找出100以内所有素数,存放于数组prime中。并将所找到的数,按每行10个的形式显示在窗体上

2024-10-28 23:25:34
推荐回答(1个)
回答1:

Option Explicit
Private Sub Form_Click()
Dim i As Long, j As Long, n As Integer, prime() As Integer
For i = 1 To 100
If fc(i) Then
n = n + 1
ReDim Preserve prime(1 To n)
prime(n) = i
End If
Next

For i = 1 To n
Print prime(i);
If i Mod 10 = 0 Then Print
Next

Print: Print "共有:" & n
End Sub
Function fc(a) As Boolean
Dim i As Long
For i = 2 To Int(a / 2)
If a Mod i = 0 Then Exit For
Next
If i = Int(a / 2) + 1 Then fc = True
End Function