用子程序产生1~200的素数

来源:百度知道 编辑:UC知道 时间:2024/06/06 06:20:18
刚学的VB,用最简单的方法做出来就好了……

写了一下。。

调用:
call prime(1,200)

过程代码:

sub prime(a,b)
for i = a to b
if nf(i) then
print i & "," ;
next i
end

Function nf(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
Exit For
End If
Next I
If I > Int(Sqr(n)) Then '如果正常跳出For循环,则I跳出循环For后的值应该是N,所以满足这个条件
s = True '当I是素数时s=true
End If
nf = s '当I是素数时, nf=true,否则 nf=false
End Function

要是回答的内容有问题,或认为不妥,请发送百度消息给我,消息内容加上本页网址哦。。

·