求100以内素数的和

来源:互联网 发布:c语言九九乘法表的输出 编辑:程序博客网 时间:2024/04/29 20:38

Private Sub Command1_Click()

 Dim s As Integer, n As Integer

  s = 0: n = 1

 Do While n <= 100

 If IsPrimer(n) <> 0 Then

      s = s + n

   End If

   n = n + 1

 Loop

 Print "1~100之内的素数的和是:" + Str(s)

  

'Label1.Caption = IIf(IsPrimer(Int(Val(Text2.Text))) = 0, "bushi", "shi”)

End Sub

Private Function IsPrimer(n As Integer) As Integer

 Dim i As Integer

   For i = 2 To n - 1

      If n Mod i = 0 Then

       Exit For

      End If

   Next i

   

   If i = n Then

       IsPrimer = n

   Else

       IsPrimer = 0

   End If

   

End Function

原创粉丝点击