子題2(8%):如果有一個正整數n,其值等於所有n 的因數(除了n 以外)之總合,則n 稱為「完美數」(Perfect number)。在此計算中,其「因數」不限制為「質因數」。請輸出2 到數字k 之間的完美數。
輸入說明:
第1 行有1 個數字,代表k 的值。而k 的值不超過50000。
輸出說明:
每行輸出1 個範圍內的完美數,依其值由小到大輸出。
輸入範例:【檔名:in-5-2.txt】
10000
輸出範例:【檔名:out-5-2.txt】
6
28
496
8128
Dim a, i, j, ans As Long
回覆刪除Sub Main()
Open App.Path & "\in-5-2.txt" For Input As #1
Open App.Path & "\out-5-2.txt" For Output As #2
Input #1, a
For i = 2 To a
ans = 1
For j = 2 To Sqr(i - 1)
If i Mod j = 0 Then ans = ans + j + (i / j)
Next
If ans = i Then Print #2, Trim(ans)
Next
End Sub
頓了一下!!
回覆刪除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
If Total(i) = i Then Print #2, Trim(i)
Next i
Close #2
Close #1
End
End Sub
Function Total(A) '
T = 0
For i = 1 To A - 1
If A Mod i = 0 Then T = T + i
Next i
Total = T
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 = 2 To n
Call A1(i)
Next
Close
Close
End
End Sub
Sub A1(a)
For i = 1 To a - 1
If a Mod i = 0 Then t = t + i
Next
If t = a Then Print #2, a
End Sub