Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, n For i = 1 To n Input #1, x
For j = 2 To (x - 1) / 2 If Check(j) = Check(x - j) And Check(j) = False Then Print #2, j & " " & x - j: Exit For Next j
Next i Close #2 Close #1 End End Sub
Function Check(A) P = False For i = 2 To A - 1 If A Mod i = 0 Then P = True: Exit For Next i Check = P End Function
Private Sub Form_Load() Me.Hide Open App.Path & "\in.txt" For Input As #1 Open App.Path & "\out.txt" For Output As #2 Input #1, t For i = 1 To t Input #1, n Call A1(n, n) Next Close Close End End Sub
Sub A1(a, b) Dim ch As Boolean For i = 2 To b ch = True For j = 2 To i - 1 If i Mod j = 0 Then ch = False Next If ch = True Then If A2(a, i) = 1 Then Exit Sub Next End Sub
Function A2(a, b) Dim ch As Boolean ch = True For i = 2 To (a - b) - 1 If (a - b) Mod i = 0 Then ch = False Next If ch = True Then Print #2, b; i: A2 = 1 End Function
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, n
For i = 1 To n
Input #1, x
For j = 2 To (x - 1) / 2
If Check(j) = Check(x - j) And Check(j) = False Then Print #2, j & " " & x - j: Exit For
Next j
Next i
Close #2
Close #1
End
End Sub
Function Check(A)
P = False
For i = 2 To A - 1
If A Mod i = 0 Then P = True: Exit For
Next i
Check = P
End Function
Private Sub Form_Load()
回覆刪除Me.Hide
Open App.Path & "\in.txt" For Input As #1
Open App.Path & "\out.txt" For Output As #2
Input #1, t
For i = 1 To t
Input #1, n
Call A1(n, n)
Next
Close
Close
End
End Sub
Sub A1(a, b)
Dim ch As Boolean
For i = 2 To b
ch = True
For j = 2 To i - 1
If i Mod j = 0 Then ch = False
Next
If ch = True Then If A2(a, i) = 1 Then Exit Sub
Next
End Sub
Function A2(a, b)
Dim ch As Boolean
ch = True
For i = 2 To (a - b) - 1
If (a - b) Mod i = 0 Then ch = False
Next
If ch = True Then Print #2, b; i: A2 = 1
End Function
柯佑好,
回覆刪除你的程式正確。
在判斷是否為質數的地方,函數傳回值,以真為質數,假為否,那麼在If Check(j) = Check(x - j) And Check(j) = False Then
這行就可以改成 if check(j) and check(x-j) then
arro好,
程式正確。
程式雖然利用了二層呼叫,卻沒有讓程式變快,可惜了。
而且,1是不是質數呢? 如果是,你的程式中,不包含1。
還好,找了找網路,1不是質數。
但是,這樣的小地方要注意哦。
熊掌好,
回覆刪除if check(j) and check(x-j) then
未設定時是=true吧~
1好像叫做"合數"的樣子
回覆刪除