2012年11月20日 星期二

偶數分解



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

2 則留言:

  1. Dim num, mycount As Integer

    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
    Input #1, num
    For i = 1 To num
    For ii = 1 To i
    If i Mod ii = 0 Then mycount = mycount + 1
    If mycount > 2 Then Exit For
    Next
    If mycount = 2 Then
    mycount = 0
    For iii = 1 To num - i
    If (num - i) Mod iii = 0 Then mycount = mycount + 1
    If mycount > 2 Then Exit For
    Next
    If mycount = 2 Then Print #2, num & "=" & i & "+" & num - i: Exit For
    End If
    mycount = 0
    Next
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. 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
    Input #1, x
    For q = 1 To x
    w = 0
    y = 0
    For j = 1 To (x - q)
    If (x - q) Mod j = 0 Then y = y + 1
    Next
    For k = 1 To q
    If q Mod k = 0 Then w = w + 1
    Next
    If w = 2 And y = 2 Then
    Print #2, x & "=" & q & "+" & (x - q)
    Exit For
    End If
    Next
    Loop Until EOF(1)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除