-6

I try To find a solution to a question .... we have a number , example : 20 ... and we have 6 number :{ a ,b , c , d , e , f} < 20 , t try to find all values of these numbers , but only if we can combinate (whit + or -) whit 2 of this numbers and getting all the value below to 20 : for example

we choose 31 :

a = 22 b = 21 c = 14 d = 11 e = 9 f = 5

we have : 22 - 21 = 1 ; 11 - 9 = 2 ; 14 - 11 = 3 ; 9 - 5 = 4 ; f = 5 ; 11 - 5 = 6 ; 21 - 14 = 7 ; .... .... .... .... .... 21 + 9 = 30 ; 9 + 22 = 31 ;

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
user336671
  • 341
  • 2
  • 5
  • 9
  • 1
    Looks like you're on the right path. The numbers add up. – Tim Lloyd May 31 '10 at 19:26
  • You should tag this as homework, if it is one. Have a look at the faq: http://meta.stackexchange.com/questions/10811/how-to-ask-and-answer-homework-questions – soulmerge May 31 '10 at 19:26
  • 1
    The question could use an English proof check - it is unreadable. – Danny Varod May 31 '10 at 19:31
  • 4
    I'm not sure that I understand your question. Is the problem that you are working on: find combinations of 6 numbers such the set of differences between two of the numbers union the set of sums of two of the numbers has, as a subset, {1, 2, 3, 4, 5, ..., 17, 18, 19, 20}? – Daniel Trebbien May 31 '10 at 19:35
  • Daniel Trebbien : That's it ane these 6 numbers < 20 – user336671 May 31 '10 at 19:41
  • I edited the question to parse better in English; if I've misrepresented what your question was, please revert my edit. – Daniel Vandersluis May 31 '10 at 19:46
  • @Daniel Vandersluis, your re-write of the problem is different than the original. @the-ifl's problem was: given a number n, find all combinations of 6 numbers a, b, c, d, e, and f such that {a, b, c, d, e, f} ∪ {a+b, a+c, ..., a+f, b+c, b+d, ..., b+f, ..., e+f} ∪ {a-b, a-c, ..., a-f, b-c, b-d, ..., b-f, ..., e-f} contains 1, 2, 3, 4, ..., n-1, and n. – Daniel Trebbien May 31 '10 at 19:51
  • @Daniel Trebbien, I misunderstood the question then. I've reverted my edit. – Daniel Vandersluis May 31 '10 at 19:53
  • @the-ifl, are you excluding negative numbers and 0 when selecting a, b, c, d, e, and f? – Daniel Trebbien May 31 '10 at 20:00

3 Answers3

5

This appears like homework, so I'll try to just give you some hints.

You want to loop over all combinations of two numbers from the array. To do that, you can use an outer loop that loops over all the numbers and then inside that loop, an inner loop that also loops over all the numbers.

Depending on exactly what you want to do, you need to decide if you want to handle x, y separately from y, x

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187
1

Given a natural number n, find all combinations of 6 positive integers a, b, c, d, e, and f such that {a, b, c, d, e, f} ∪ {a + b, a + c, ..., a + f, b + c, b + d, ..., b + f, ..., e + f} ∪ {a - b, a - c, ..., a - f, b - c, b - d, ..., b - f, ..., e - f, b - a, c - a, ..., f - a, c - b, d - b, ..., f - b, ..., f - e} contains 1, 2, 3, 4, ..., n - 1, and n.

I haven't thought about this problem very much, but it appears to me to be a hard problem, indeed. Maybe there is a trick, I am not sure, but if I had to solve this problem, I would first try to find the least n for which there are not infinitely-many solutions (if n is 1, for example, then any combination of 6 natural numbers containing two consecutive natural numbers will work).

I think that you will have better luck with finding a solution to this problem in a mathematics discussion forum.

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
0
void FindCombinations(int n, int [] values)
{
   for(int i = 0; i < values.length; i++)
   {
     int left = values[i];
     for(int j = 0; k < values.length; j++)
     {
       int right = values[j];

       if (left == right)
          continue;

       if (left + right < n)
          Console.Write(string.Format("{0} + {1} = {2} < {3}", left, right, left+right, n);
       if (left - right < n)
          Console.Write(string.Format("{0} - {1} = {2} < {3}", left, right, left-right, n);
     }
   }
}

While this question stinks of homework it's not really that hard (although arguably hard to interpret).

Note: This solution is O(n^2) and is not efficient, please keep this in mind.

Aren
  • 54,668
  • 9
  • 68
  • 101