Disclaimer: This is not a homework problem. I stumbled upon this puzzle here and I also have the answer. However, I am not able to figure out an approach to arrive at the solution.
The puzzle is as given below:
The product of the ages of David’s children is the square of the sum of their ages. David has less than eight children. None of his children have the same age. None of his children is more than 14 years old. All of his children is at least two years old. How many children does David have, and what are their ages?
The answer happens to be 2,4,6,12.
Please suggest a way to solve this problem programmatically.
I solved it in java using a recursive approach.
First the program prints all the combinations, then gives the correct combination (that matches the specified criteria) at last.
This program instantly gives the output
just as you have specified in your question.
The output obtained was