Private Function prime(m As Long)
Dim f As Boolean
f = True
If m > 3 Then
For i = 1 To Sqr(m)
If m Mod i = o Then f = False: Exit For
Next
End If
prime = f
End Function
这个地方,为什么要用到Sqr(m),还有后面为什么用next(想问一下i
是可以省略么),还有最后为什么将f的值赋给prime?
Private Sub Command1_Click()
Dim n, x, y As Long
n = Val(Text1.Text1)
If n < 6 Or n Mod 2 <> 0 Then
MsgBox "必须输入大于6的偶数"
Cancel = True
Else
For x = 3 To n / 2 Step 2
If prime(x) Then
y = n - x
If prime(y) Then
Text(2).Text1 = x & "+" & y
Exit For
End If
End If
Next
End If
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End Sub
还有这里的If prime(x) Then,意思应该是如果函数运行么?我经常看到if p then (p是Boolean),是不是也是省略形式,即为f p= true then ?谢谢了。