求100到999之间最大的三个素数的 vb程序代码
3个回答
2009-04-30
展开全部
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
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 lTmp = 0 Then Exit For
Next lTmp
If lTmp > Int(Sqr(lNum)) Then isPrime = True
End If
End Function
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 lTmp = 0 Then Exit For
Next lTmp
If lTmp > Int(Sqr(lNum)) Then isPrime = True
End If
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Dim a(800)
Private Sub Command1_Click()
For i = 900 To 100 Step -1
For j = 2 To i - 1
If i \ j = i / j Then GoTo m
Next j
a(t) = i
t = t + 1
If t Mod 15 = 0 Then Print
Print i & " ";
m:
Next i
Print
Print a(0), a(1), a(2) '由于排列是a(i)就是有大到小排列的。那么只要在整体素数的排列中取前三个数值就行了。
End Sub
程序最大的弊端就是使用的内存浪费了很多!
Private Sub Command1_Click()
For i = 900 To 100 Step -1
For j = 2 To i - 1
If i \ j = i / j Then GoTo m
Next j
a(t) = i
t = t + 1
If t Mod 15 = 0 Then Print
Print i & " ";
m:
Next i
Print a(0), a(1), a(2) '由于排列是a(i)就是有大到小排列的。那么只要在整体素数的排列中取前三个数值就行了。
End Sub
程序最大的弊端就是使用的内存浪费了很多!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询