求用VB , for next 语句写一个1到100之间的质数的和

来源:百度知道 编辑:UC知道 时间:2024/05/17 16:19:40

Private Sub Form_click()
Cls
Dim sum As Integer
Dim n As Integer
For I = 1 To 100
If Pd(I) = True Then
sum = sum + I
n = n + 1
Print I;
x = x + 1
If x Mod 10 = 0 Then Print
End If
Next
Print
Print "素数的和为:" & sum
Print "共有" & n & "个"
End Sub
Private Function Pd(ByVal n As Integer) As Boolean
Dim I As Integer
For I = 2 To n - 1
If n Mod I = 0 Then Exit For
Next I
If I >= n Then
Pd = True
Else
Pd = False
End If
End Function