vb随机产生10个两位数随机正整数,求其中素数之和(用过程实现判断一个数是否是素数)

2025-04-30 01:06:27
推荐回答(2个)
回答1:

Dim yes As Boolean

Private Sub Command1_Click()

  Randomize

  s = 0

  For i = 1 To 10

    x = Int(90 * Rnd) + 10

    Print x;

    ss x

    If yes Then s = s + x

  Next i

  Print

  Print "其中素数的总和="; s

End Sub


Private Sub ss(ByVal x As Integer)

    yes = True

    For j = 2 To Sqr(x)

      If x Mod j = 0 Then yes = False: Exit For

    Next j

End Sub

回答2:

Public Function IsPrime(ByVal x As Integer) As Boolean
    Dim i As Integer
    IsPrime = True
    For i=2 To x-1
        If (x Mod i) = 0 Then
             IsPrime = False
            Exit Function
        End If
    Next
End Function

Private Sub Command1_Click()
    Dim a As Integer, r As Integer, s As Integer
    s = 0
    For a=1 To 10
        r = Int(90 * Rnd + 10)
        If IsPrime(r) Then s = s + r
   Next
   Print r
End Sub