求100以内素数的和

来源:互联网 发布:均衡器软件哪个好 编辑:程序博客网 时间:2024/06/11 19:54

Private Sub Command1_Click()

 

  Dim s As Integer, n As Integer

   s= 0: n = 1

 

 

   DoWhile 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) AsInteger

 

   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

        就是觉得让我们自己做不会做,但老师要是把步骤写出来,就能看得懂

 

 

 

 

原创粉丝点击