Interview question: find solution to x! + y! + z! = x * y * z


This blog post has moved to https://matthewvaneerde.wordpress.com/2008/08/07/interview-question-find-solution-to-x-y-z-x-y-z/

Comments (7)

Cancel reply

  1. Zbyszek says:

    we can assume that a>=b>=c (if not rearrange them)

    for a>=5 there are not solutions as 6!=720 > a*a*a, hence the program below:

           static void Main(string[] args)

           {

               int[] F = new int[6];

               for (int a = 0; a < 6; a++)

               {

                   var fa = F[a] = a==0 ? 1 : F[a-1]*a;

                   for (int b = 0; b <= a; b++)

                   {

                       var fb = F[b];

                       for (int c = 0; c <= b; c++)

                       {

                           var fc = F[c];

                           if (fa + fb + fc == a*b*c)

                               Console.WriteLine("{0} {1} {2}", a, b, c);

                       }

                   }

               }

               Console.ReadLine();

           }

    I hope you find this efficient :)

  2. You nailed it.  I want to expound a little more on this in a future post.

  3. Zbyszek says:

    My son just noticed that I should have started all loops from 1 not from 0 as a*b*c would be zero if a or b or c is zero.

    It saves a bit :)

    The other optimisations would be:

    1/ keep a*b where fb is calculated

    2/ notice that except 1! all factorials are even, so all a,b,c cannot be all odd at the same time

    But somehow, I have a feeling that all those optimisations have not a lot of sense, bearing in mind that the whole algorithm takes couple of microseconds anyway :)

  4. n says:

    NOT (x+y) = not X + NOT y ….

    why is this ?

  5. > NOT (x+y) = not X + NOT y

    Sorry, I don’t follow.  Is this a symbolic logic question: "~(x v y) = ~x v ~y"?  If so, that’s false; De Morgan’s law requires flipping v to ^ and vice versa: "~(x v y) = ~x ^ ~y".

    If it’s just a general philosophical observation, then, you go, girl.

  6. To be more explicit (using ~ to denote logical negation, to avoid confusion with the factorial symbol:)

    0 0 1 1 : x

    0 1 0 1 : y

    1 1 0 0 : ~x

    1 0 1 0 : ~y

    0 1 1 1 : x v y

    1 0 0 0 : ~(x v y)

    1 1 1 0 : ~x v ~y

    1 0 0 0 : ~x ^ ~y

    Note that ~(x v y) is not always equal to ~x v ~y, but is always equal to ~x ^ ~y.

Skip to main content