求100到999之间最大的三个素数的 vb程序代码

来源:百度知道 编辑:UC知道 时间:2024/06/15 12:47:19

Private Sub Command1_Click()
Dim n As Integer, i As Integer, j As Integer, n1 As Integer
Dim flag As Boolean
n1 = 1
For n = 900 To 100 Step -1
flag = True
For i = 2 To Int(Sqr(n))
If n Mod i = 0 Then
flag = False
Exit For
End If
Next
If flag Then
Do While n1 <= 3
Print n;

n1 = n1 + 1
Exit Do
Loop

End If

Next

End Sub

Private Sub Command1_Click()
Dim i As Long, k As Integer
For i = 999 To 100 Step -1
If isPrime(i) Then
k = k + 1
Debug.Print "第"; k; "个素数 "; i
If k = 3 Then Exit For
End If
Next i
End Sub
Private Function isPrime(lNum As Long) As Boolean
Dim lTmp As Long
isPrime = False '可以不写该句
If lNum = 2 Then
isPrime = True
ElseIf lNum > 2 And lNum Mod 2 <> 0 Then
For lTmp = 3 To Int(Sqr(lNum)) Step 2
If lNum Mod