2012年4月25日 星期三

偶數分解


把一個偶數 n 分解成兩個質數,並輸出任意一組即可。 輸出格式請參考輸出範例。 2  n  2 ^ 31 - 1

輸入範例:

10
20
22
12
52
4
100
48
32
10000000


輸出範例:

10 = 3 + 7
20 = 3 + 17
22 = 3 + 19
12 = 5 + 7
52 = 5 + 47
4 = 2 + 2
100 = 3 + 97
48 = 5 + 43
32 = 3 + 29
10000000 = 29 + 9999971

3 則留言:

  1. Dim a As Boolean
    Dim j, i, x, n As Long
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    a = True
    Input #1, x
    For i = 2 To x
    For j = 2 To x - 1
    If i + j = x Then Call b(a)
    Next
    If a = True Then Exit For
    Next
    Loop
    Close
    Close
    End
    End Sub
    Function b(c)
    a = True
    For g = 2 To i - 1
    If i Mod g = 0 Then a = False: Exit For
    Next
    For h = 2 To j - 1
    If a = False Then Exit For
    If j Mod h = 0 Then a = False: Exit For
    Next
    If a = True Then Print #2, x & "=" & i & "+" & j
    End Function

    回覆刪除
  2. 好快阿 開始訓練哩~ 加油囉

    回覆刪除
  3. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, n
    q = Val(n)

    For j = 2 To q
    pan = True
    For i = 2 To (j - 1) / 2
    If j Mod i = 0 Then pan = False
    Next i
    If pan = True Then
    a = j
    r2 = q - a
    duan = True
    For i = 2 To (r2 - 1) / 2
    If r2 Mod i = 0 Then duan = False
    Next i
    If duan = True Then
    ans = q & " = " & a & " + " & r2
    Print #2, ans
    Exit For
    End If
    End If
    Next j
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除