2011年5月30日 星期一

偶數分解

  把一個偶數 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


5 則留言:

  1. 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

    For i = 2 To n
    If i + (n - i) = n And Func(i) = True And Func(n - i) = True Then Print #2, n & " = " & i & " + " & (n - i): Exit For
    Next

    Loop

    Close
    Close
    End
    End Sub

    Function Func(a)

    k = True
    For i = 2 To a / 2
    If a Mod i = 0 Then k = False
    Next
    Func = k
    End Function

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\out.txt" For Output As #2
    Open App.Path & "\in.txt" For Input As #1

    Do Until EOF(1)
    Input #1, n

    If n = 4 Then
    a = 2: b = 2
    Else
    For i = n - 1 To 2 Step -1
    If cc(i) = False Then
    If cc(n - i) = False Then a = i: b = n - i: Exit For
    End If
    Next
    End If
    Print #2, n & " = " & a & " + " & b
    Loop

    Close #1
    Close #2
    End
    End Sub
    Function cc(a1)
    gm = False
    If a1 = 1 Then gm = True
    For i = 2 To a1 / 2
    If a1 Mod i = 0 Then gm = True
    Next
    cc = gm
    End Function
    -----------------
    in.txt
    10
    20
    22
    12
    52
    4
    100
    48
    32
    10000000
    -----------------

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

    回覆刪除
  3. Dim T As Boolean
    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, ans
    T = False
    Call ABC(ans, ans & "=")
    Loop
    Close #2
    Close #1

    End
    End Sub

    Function ABC(A, B)
    If A = 0 Then
    Print #2, Left(B, Len(B) - 1): T = True
    Else
    For i = A To 2 Step -1
    p = True
    For j = 2 To i - 1
    If i Mod j = 0 Then p = False
    Next j
    If p = True Then
    If Mplus(B) <= 1 Then Call ABC((A - i), (B & i & "+"))
    If T = True Then Exit For
    End If
    Next i
    End If
    End Function

    Function Mplus(A)
    For i = 1 To Len(A)
    If Mid(A, i, 1) = "+" Then ans = ans + 1
    Next i
    Mplus = ans
    End Function

    -----------------
    in.txt
    10
    20
    22
    12
    52
    4
    100
    48
    32
    10000000
    -----------------

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

    ----------------------
    一不小心做成

    10=7+3
    10=5+5
    10=5+3+2
    10=5+2+3
    10=3+7
    10=3+5+2
    10=3+3+2+2
    10=3+2+5
    10=3+2+3+2
    10=3+2+2+3
    10=2+5+3
    10=2+3+5
    10=2+3+3+2
    10=2+3+2+3
    10=2+2+3+3
    10=2+2+2+2+2

    回覆刪除
  4. arro,緣尉,佑好,
    3個程式都正確。
    佑,如果在解題的過程式,發現可以做的類似題,那就再出一題,並寫出程式啊。

    回覆刪除
  5. arro好,
    上次有說過的
    If i + (n - i) = n
    這樣的多餘的動作,要小心些哦。

    回覆刪除