Results 1 to 3 of 3

Thread: The complete numbers

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2006
    Posts
    384

    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

  2. #2
    Lively Member
    Join Date
    Aug 2005
    Posts
    74

    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.

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: The complete numbers

    VB Code:
    1. Public Function PerfectNumber(ByVal Number As Long) As Boolean
    2. Dim laFactor() As Long
    3. Dim lDenom As Long
    4. Dim lCounter As Long
    5. Dim lSum As Long
    6.    
    7.     'Perfect Numbers must be even
    8.     If Number Mod 2 = 1 Then Exit Function
    9.    
    10.     'Seed the array with the values 1 + 2
    11.     ReDim laFactor(0 To 1)
    12.     laFactor(0) = 1
    13.     laFactor(1) = 2
    14.    
    15.     'Loop through all number fro 3 to the mid-point
    16.     For lDenom = 3 To CLng(Number / 2)
    17.        
    18.         'If the number is a factor..
    19.         If Number Mod lDenom = 0 Then
    20.            
    21.             '..add it to the array
    22.             ReDim Preserve laFactor(UBound(laFactor) + 1)
    23.             laFactor(UBound(laFactor)) = lDenom
    24.         End If
    25.     Next lDenom
    26.    
    27.     'Sum up the factors
    28.     For lCounter = 0 To UBound(laFactor)
    29.         lSum = lSum + laFactor(lCounter)
    30.     Next lCounter
    31.    
    32.     'If the sum of factors equals the number
    33.     'It is Perfect
    34.     PerfectNumber = (lSum = Number)
    35. 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
  •  



Click Here to Expand Forum to Full Width