Page 2 of 2

Posted: Sun Sep 23, 2007 7:00 pm
by Alboin
GLneo wrote:but what if I want to test 3, 4, 5+ numbers?
I'm not really sure....Getting two digits to work in such a small amount of code was kind of hackish by itself. In fact, I'm not entirely sure how isin works... I'm thinking of two ways at the moment for multiples:
  • Expand the table array. This could get ugly for 7 some numbers...
  • Check by trying different combinations of the numbers and calling isin multiple times. Something like:

    Code: Select all

    isin2(n, a,b,c) {
       isin(n, a+b, c)
       isin(n, a+c, b)
       isin(n, c+b, a)
       ...
       Check for certain condition...
    }
    
    I don't know if this method actually works...

Posted: Sun Sep 23, 2007 7:26 pm
by GLneo
O, there has to be a better way, with loops or something...

my brain hurts...

Posted: Mon Sep 24, 2007 2:39 pm
by jnc100
If C#'s your thing:

Code: Select all

        static int[] NotSummable(int[] originals, int limit)
        {
            List<int> l = new List<int>(originals);
            List<int> ret = new List<int>();

            for (int i = 1; i < limit; i++)
            {
                bool found = false;
                foreach (int orig in l)
                {
                    if (l.Contains(i - orig) || l.Contains(i))
                    {
                        l.Add(i);
                        found = true;
                        break;
                    }
                }

                if (!found)
                    ret.Add(i);
            }

            return ret.ToArray();
        }
Regards,
John.

Posted: Mon Sep 24, 2007 10:47 pm
by Solar
{ algorithm deleted due to severe thinko }

Posted: Wed Sep 26, 2007 2:11 pm
by GLneo
simple problem, but hard to do with computers :P