2012年4月27日 星期五

傳統數學問題的解決


如果有一個正整數n,其值等於所有n 的因數(除了n 以外)之總合,則n 稱為「完美數」(Perfect number)。在此計算中,其「因數」不限制為「質因數」。請輸出2 到數字k 之間的完美數。

輸入說明:
第1 行有1 個數字,代表k 的值。而k 的值不超過50000。

輸出說明:
每行輸出1 個範圍內的完美數,依其值由小到大輸出。

輸入範例:【檔名:in.txt】
10000
輸出範例:【檔名:out.txt】

6
28
496
8128

4 則留言:

  1. Private Sub Form_Load()
    Dim ans As Double
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, x
    For i = 2 To x
    ans = 0
    For j = 1 To i / 2
    If i Mod j = 0 Then ans = ans + j
    Next
    If ans = i Then Print #2, ans
    Next
    Close
    Close
    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
    Input #1, k
    For i = 2 To k
    n = 0
    For j = 1 To i / 2
    If i Mod j = 0 Then n = n + j
    Next j
    If n = i Then Print #2, n
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  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
    Input #1, n
    For i = 2 To n
    x = 0
    For j = 1 To (i / 2)
    If i Mod j = 0 Then x = x + j
    Next j
    If i = x Then Print #2, x
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  4. 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, r
    a = 2
    Do
    ans = 0
    For i = 1 To a
    If i <> a Then
    If a Mod i = 0 Then ans = ans + i
    End If
    Next i
    If ans = a Then Print #2, a
    a = a + 1
    Loop Until a = Val(r)
    Close #2
    Close #1
    End
    End Sub

    回覆刪除