1、所谓完全数就是这个数数恰好等于它的因子之和;
2、从算法上而言,你需要做两件事情:
求一个数的所有的因子(或者称为约数,但不包括自己);
求所有因子的和并和自身对比
3、前者的算法很多,后者求和对比更容易,不知道你要哪一种语言的程序。
午休抽了几分钟,在Excel的VBA里写了一个简单的程序:
Sub main() ‘主程序,从2-10000进行对比,如果相同则是完全数
Dim i As Integer
For i = 2 To 10000
If Summary_divisors(i) = i Then
j = j + 1
Cells(j, 1) = i
End If
Next i
End Sub
Function Summary_divisors(iNumber As Integer) As Integer ’求出约数的和并返回。
Dim i As Integer
Dim Remainder As Integer
If iNumber <= 1 Then
Summary_divisors = 0
Else
For i = 1 To Int(iNumber / 2)
Remainder = iNumber Mod i
If Remainder = 0 Then
Summary_divisors = Summary_divisors + i
End If
Next i
End If
End Function
算出来的完全数不多:
6
28
496
8128