vb程序:编写一个过程用来判断某个整数是否是素数,
再编写一个过程用来判断1000以内是否存在连续素数(两数相差为2),如果存在则把他们输出出来....
再编写一个过程用来判断1000以内是否存在连续素数(两数相差为2),如果存在则把他们输出出来.
展开
1个回答
展开全部
Public Function IsSushu(ByVal pa As Integer) As Boolean
Dim pb As Integer = pa - 1
Dim res As Boolean = True
For i = 2 To pb
If pa Mod i = 0 Then
res = False
Exit For
End If
Next i
Return res
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim pre As Integer
Dim str As String
str = ""
pre = 2
For i = 1 To 1000
If IsSushu(i) Then
If i - pre = 2 Then
str = str & i & " " & pre & vbCrLf
End If
pre = i
End If
Next
TextBox1.Text = str
End Sub
运行结果:
5 3
7 5
13 11
19 17
31 29
43 41
61 59
73 71
103 101
109 107
139 137
151 149
181 179
193 191
199 197
229 227
241 239
271 269
283 281
313 311
349 347
421 419
433 431
463 461
523 521
571 569
601 599
619 617
643 641
661 659
811 809
823 821
829 827
859 857
883 881
Dim pb As Integer = pa - 1
Dim res As Boolean = True
For i = 2 To pb
If pa Mod i = 0 Then
res = False
Exit For
End If
Next i
Return res
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim pre As Integer
Dim str As String
str = ""
pre = 2
For i = 1 To 1000
If IsSushu(i) Then
If i - pre = 2 Then
str = str & i & " " & pre & vbCrLf
End If
pre = i
End If
Next
TextBox1.Text = str
End Sub
运行结果:
5 3
7 5
13 11
19 17
31 29
43 41
61 59
73 71
103 101
109 107
139 137
151 149
181 179
193 191
199 197
229 227
241 239
271 269
283 281
313 311
349 347
421 419
433 431
463 461
523 521
571 569
601 599
619 617
643 641
661 659
811 809
823 821
829 827
859 857
883 881
更多追问追答
追问
运行不出来啊
追答
我运行出来了呀,你是什么错误?另外你用的vb6还是vb2010?
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询