Results 1 to 5 of 5

Thread: Algorithm to get closest number set

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    101

    Question Algorithm to get closest number set

    Good day,

    Let say we hv a number set {11, 22, 33, 44, 55, 66, 77, 88, 99, 111}. Is there any algorithm that can give me a subset {x1, x2, x3, ...} where the sum of the subset closest and less than to 150 , for example ?

    E.g.,

    11+22 = 33
    11+22+33+44 = 110
    11+22+33+44+55 = 165

    So the answer using the above example would be {11, 22, 33, 44} as it gives the closest 10 150. Is there any algorithm to get this set ?

    Thanks.

    SonicWave
    Last edited by SonicWave; Jul 22nd, 2006 at 03:07 AM.

  2. #2
    Lively Member
    Join Date
    Dec 2005
    Posts
    92

    Re: Algorithm to get closest number set

    this is probably way too messy and can be done better BUT

    VB Code:
    1. Dim answer As String
    2.     Dim counter As Integer
    3.     Dim sum As Integer
    4.     Dim goal As Integer
    5.     Dim i As Integer
    6.     Dim myArray(10) As Integer
    7.     myArray(0) = 11
    8.     myArray(1) = 22
    9.     myArray(2) = 33
    10.     myArray(3) = 44
    11.     myArray(4) = 55
    12.     myArray(5) = 66
    13.     myArray(6) = 77
    14.     myArray(7) = 88
    15.     myArray(8) = 99
    16.     myArray(9) = 111
    17.     counter = 0
    18.     sum = 0
    19.     goal = 150
    20.     answer = ""
    21.     i = 0
    22.    
    23.     Do While counter < (UBound(myArray()) - 1)
    24.         sum = sum + myArray(counter)
    25.  
    26.         If sum = goal Then
    27.                 Do While i <= counter
    28.                     answer = answer & " " & myArray(i)
    29.                     i = i + 1
    30.                 Loop
    31.            
    32.             MsgBox ("{" & answer & "}" & " added together = " & sum & " which is closest to and less than " & goal)
    33.             Exit Do
    34.         End If
    35.            
    36.         If sum > goal Then
    37.             sum = sum - myArray(counter)
    38.                 Do While i <= counter
    39.                     answer = answer & " " & myArray(i)
    40.                     i = i + 1
    41.                 Loop
    42.             MsgBox ("{" & answer & "}" & " added together = " & sum & " which is closest to and less than " & goal)
    43.             Exit Do
    44.         End If
    45.         counter = counter + 1
    46.     Loop

  3. #3
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: Algorithm to get closest number set

    Is it necessary that your subset contain only consecutive members of the number set?

    If not, then the solution for the example number set and target sum is:
    11 + 22 + 111 = 144
    33 + 111 = 144

    A simple recursive subroutine can easily find all these sums.

  4. #4
    Lively Member
    Join Date
    Dec 2005
    Posts
    92

    Re: Algorithm to get closest number set

    I think so Logo; the way he gives examples shows that.

    Also "subset {x1, x2, x3, ...} " implies consecutive members.

  5. #5
    pathfinder NotLKH's Avatar
    Join Date
    Apr 2001
    Posts
    2,397

    Re: Algorithm to get closest number set

    But must the first element of the subset be the first element of the set?

    In other words, could {66 77} be considered?

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