用VB编程:编写一个函数判断已知数M,是否为“完”数

来源:百度知道 编辑:UC知道 时间:2024/05/11 02:54:02
谢谢各位。。。
“完数”为该因子之和(如6=1+2+3)
就是我要输入一个数,判断下是不是“完”数。。。
谢谢。。。下面的哪个答案我做不来。。。
还有么。。。
好急啊。。。

Function isPerfect(num As Integer) As Boolean '判断变量num是否为完数
Dim i As Integer, S As Integer
S = 0
For i = 1 To num / 2
If num Mod i = 0 Then
S = S + i
End If
Next
isPerfect = (S = num)
End Function

Sub test() '列出1到20000之间的所有完数
Dim i As Integer
For i = 1 To 20000
If isPerfect(i) Then
Debug.Print i
End If
Next
End Sub

得到:
6
28
496
8128

Public Function ws(m As Long) As Boolean
Dim i As Long
Dim ls As Long
For i = m / 2 To 1 Step -1
If m Mod i = 0 Then ls = ls + i
Next i
If ls = m Then
ws = True
Else
ws = False
End If
End Function

这个对吗?