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

You must be logged in to post a comment.

This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use. Learn more

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

You must be logged in to post a comment.

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 :)

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

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 :)

Follow-up here:

http://blogs.msdn.com/matthew_van_eerde/archive/2008/08/08/solution-x-y-z-x-y-z.aspx

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

why is this ?

> 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.

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.