'要求编写的程序
Private Sub C1_Click()
a% = 18000
Do
a% = a% - 1
Loop Until isprime(a%)
Text1.Text = a%
End Sub
Private Sub C2_Click()
Open "out5.txt" For Output As #1
Print #1, Text1.Text
Close #1
End Sub
'提供的程序
Private Function isprime(a As Integer) As Boolean
Dim flag As Boolean
flag = True
b% = 2
Do While b% <= Int(a / 2) And flag
If Int(a / b%) = a / b% Then
flag = False
Else
b% = b% + 1
End If
Loop
isprime = flag
End Function
谁能详细的帮我说一下,谢谢!!
參考答案:Private Sub C1_Click()
a% = 18000
Do
a% = a% - 1 '由18000开始逐步减1,直至找到素数退出循环
Loop Until isprime(a%) '调用isprime()函数过程判断a%是否素数,是则退出循环
Text1.Text = a%
End Sub
此过程是由18000往下找一个素数,找到了就立即退出循环,并把此数显示在Text1中。
Private Sub C2_Click()
Open "out5.txt" For Output As #1
Print #1, Text1.Text
Close #1
End Sub
此过程是将找到的素数写进名为out5.txt的文本文件里。
Private Function isprime(a As Integer) As Boolean
Dim flag As Boolean
flag = True
b% = 2
Do While b% <= Int(a / 2) And flag
If Int(a / b%) = a / b% Then
flag = False
Else
b% = b% + 1
End If
Loop
isprime = flag
End Function
这个函数是用来判别一个数是否是素数的。