Angerstein wrote at Wed, 18 Sep 2002 17:20:29 +0200: > if you have a list of numbers: > ... > What you really want are groups each with 3 numbers. > > The summe of the numbers in a group should be near as possible on the > average value. > > My "simple" idea: > sort the numbers. > put the first, the last and the middle number in a group and remove them > from the list. > > > Any better or even mathematically correct ideas ?
And the Perl question is .... ? You're asking for a mathematic problem, that is better posted and answered in a mathematic newsgroup, or perhaps on a computer science newsgroup. As it is an interesting problem, I'll answer it, but I'm not a good mathematician. You're algorithm seems only to work with normal distributed numbers. Let's have instead something like these numbers: 1 4 9 16 25 36 49 64 81 (average 31,67) You're algorithm would produce the groups (1 25 81) (avg 35,67) (4 36 64) (avg 34,67) (9 16 49) (avg 24,67) what doesn't seem to be a nice solution. I think, it's better to weight the sorted numbers with their sum rank, in the above example it would be: nr. sum rank 1 1 4 5 9 14 16 30 25 55 36 91 49 140 64 204 81 285 The avg of the sum rank is 285/9 = 31,67, the nr with the sum rank nearest to 31 is 16, so the 1st group is (1 16 81) (avg 32,67) Now we have nr. sum rank 4 4 9 13 25 38 36 74 49 123 64 187 The avg of the sum rank is 187/6 = 31,17, the number with the sum rank nearest to is 25, so the 2nd group is (4 25 64) (avg 31) The last group is then (9 36 49) (avg 31,33) Of course, you can also solve the real optimization problem, but for that you really should ask a mathematization. (But these algorithms are also slow :-)) Greetings, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]