|
-
Apr 2nd, 2006, 08:57 AM
#1
Thread Starter
Hyperactive Member
The complete numbers
NEED HELP WITH A HOMEWORK PLEASE
Complete numbers: Are numbers such as 6 that equals 3 + 2 + 1 (the sum of numbers it divide to).
Ok I should write a program that request a number from the user and then tells him if the number is a complete number or no, I should be using "FOR" function and ofcourse the "IF".
Thanks for help
zeid
-
Apr 2nd, 2006, 02:25 PM
#2
Lively Member
Re: The complete numbers
i really don't understand your question. What you need to do is write a program that will factor a number.Complete numbers (Perfect numbers)
The sum of factors is the original number.
6 : { 1 2 3 }, 1 + 2 + 3 = 6
28 : { 1 2 4 7 14 }, 1 + 2 + 4 + 7 + 14 = 28
496
8128
33550336
...
and then sum the factors.
To help you get started you need to take the number entered and divide it in half.
for counter = 1 to halfnumber
if number mod counter = 0 then
'counter is a factor so store it in an array
end if
next counter
now you just need to add the array values and if it equals the number then it it is a perfect number.
Last edited by geroldsh; Apr 2nd, 2006 at 03:17 PM.
-
Apr 3rd, 2006, 11:40 AM
#3
Re: The complete numbers
VB Code:
Public Function PerfectNumber(ByVal Number As Long) As Boolean
Dim laFactor() As Long
Dim lDenom As Long
Dim lCounter As Long
Dim lSum As Long
'Perfect Numbers must be even
If Number Mod 2 = 1 Then Exit Function
'Seed the array with the values 1 + 2
ReDim laFactor(0 To 1)
laFactor(0) = 1
laFactor(1) = 2
'Loop through all number fro 3 to the mid-point
For lDenom = 3 To CLng(Number / 2)
'If the number is a factor..
If Number Mod lDenom = 0 Then
'..add it to the array
ReDim Preserve laFactor(UBound(laFactor) + 1)
laFactor(UBound(laFactor)) = lDenom
End If
Next lDenom
'Sum up the factors
For lCounter = 0 To UBound(laFactor)
lSum = lSum + laFactor(lCounter)
Next lCounter
'If the sum of factors equals the number
'It is Perfect
PerfectNumber = (lSum = Number)
End Function
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|