用vb程序设计100以内孪生素数

2024-12-17 05:36:25
推荐回答(1个)
回答1:

Private Sub Command1_Click()

  Dim a(2 To 99) As Boolean

  Text1.Text = "100以内的孪生素数:" & vbCrLf

  For i = 2 To 99

    a(i) = True

  Next i

  For i = 2 To 99

    If a(i) Then

      j = i + i

      Do While j < 100

        a(j) = False

        j = j + i

      Loop

    End If

  Next i

  For i = 3 To 97 Step 2

    If a(i) And a(i + 2) Then

      Text1.Text = Text1.Text & i & "  " & i + 2 & vbCrLf

    End If

  Next i

End Sub