vb 编写fuction过程的prime函数,用来判断一个整数是否为素数然后通过调用此函数来统计2
vb编写fuction过程的prime函数,用来判断一个整数是否为素数然后通过调用此函数来统计200到300之间素数的个数。请大神解答,越简单越好,争取能让我看懂了...
vb 编写fuction过程的prime函数,用来判断一个整数是否为素数然后通过调用此函数来统计200到300之间素数的个数。
请大神解答,越简单越好,争取能让我看懂了 展开
请大神解答,越简单越好,争取能让我看懂了 展开
2个回答
展开全部
Private Sub Command1_Click()
Dim i As Integer,x as Integer
For i =200 To 300
If prime(i) = True Then x=x+1
Next i
msgbox x
End Sub
Function prime(n As Integer) As Boolean '判断n是否为素数
Dim s As Boolean
Dim i As Integer
s = False
For i = 2 To Int(Sqr(n)) '如果能被2 ~Int(Sqr(n))中任何一个数整除,则不是素数,跳出For循环
If n Mod i = 0 Then s=true: Exit For
Next i
prime =not s '当i是素数时, prime=true,否则 prime=false 和 s 相反
End Function
展开全部
Private Sub Form_Click()
Form1.AutoRedraw = True
Cls
Dim N As Integer, I As Integer
Print "200-300之间的素数有:"
For N = 200 To 300
If Prime(N) Then I = I + 1: Print N;
Next
Print
Print "共有"; I; "个"
End Sub
Function Prime(A As Integer) As Boolean
Dim X As Integer
Prime = True
For X = 2 To Sqr(A)
If A Mod X = 0 Then
Prime = False
Exit For
End If
Next
End Function
Form1.AutoRedraw = True
Cls
Dim N As Integer, I As Integer
Print "200-300之间的素数有:"
For N = 200 To 300
If Prime(N) Then I = I + 1: Print N;
Next
Print "共有"; I; "个"
End Sub
Function Prime(A As Integer) As Boolean
Dim X As Integer
Prime = True
For X = 2 To Sqr(A)
If A Mod X = 0 Then
Prime = False
Exit For
End If
Next
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询