VB算法求解

来源:百度知道 编辑:UC知道 时间:2024/07/02 19:13:35
设计一个可以判断某数是否是素数的算法。

Function isPrime(n As Long) As Boolean
Dim i as long
for i = 2 to sqr(n)
if n mod i = 0 then exit function
next
isPrime = True
End Function

if isPrime(True) Then
Msgbox "YES"
else
Msgbox "no"
end if

Function isPrime(n As Long) As Boolean
Dim i as long
for i = 2 to sqr(n)
if n mod i = 0 then exit function
next
isPrime = True
End Function

private function prime(n as integer) as Boolean
dim i as integer
for i=1 to sqr(n)
if n mod i = 0 then exit function
next i
prime=true
end function