求100到200之间的孪生素数及共有多少对?若A为素数,而A + 2也是素数,则称A和A +2是一对孪生素数,如3和5是一对孪生素数.
问题描述:
求100到200之间的孪生素数及共有多少对?
若A为素数,而A + 2也是素数,则称A和A +2是一对孪生素数,如3和5是一对孪生素数.
答
Private Sub Command1_Click()
Dim A(100) As Integer
Dim N As Integer
Dim i As Integer, j As Integer, x As Integer
For i = 100 To 200
x = 0
For j = 2 To i - 1
If i Mod j = 0 Then x = 1
Next j
If x = 0 Then
A(N) = i
N = N + 1
End If
Next i
Dim Ncont As Integer
For i = 0 To N - 1
If A(i + 1) - A(i) = 2 Then
Print A(i) & ";" & A(i + 1)
Ncont = Ncont + 1
End If
Next
Print "对数=" & Ncont
End Sub