VB 编写程序,请用户输入一个自然数N,求前N个质数,并将其存放一个数组中
问题描述:
VB 编写程序,请用户输入一个自然数N,求前N个质数,并将其存放一个数组中
(质数是除了1和本身之外没有其他约数的数,第一个质数是2)
答
Private Sub Command1_Click()
Dim a()
Dim i As Integer, k As Long
n = Val(InputBox("请输入一个正整数!"))
ReDim a(1 To n)
Do
k = k + 1
If ss(k) Then
i = i + 1
a(i) = k
End If
Loop Until i >= n
For i = 1 To n
Print a(i);
If i Mod 5 = 0 Then Print
Next i
End Sub
Function ss(n) As Boolean
If n >= 2 Then ss = True
For i = 2 To n - 1
If n Mod i = 0 Then
ss = False
Exit For
End If
Next i
End Function