## Wednesday, November 18, 2009

### Wish my math classes were like this!

Math Class (by Robert A. Kraus)

The math teacher addressed the class. "I have chosen a positive integer (base 10). It has 4 digits, none of which are 0. I have calculated the sum of its digits, the sum of the squares of its digits, and the product of its digits, and no 2 of these 3 quantities has the same number of digits. Consider the following 9 statements:
(1) The number is a prime.
(2) The sum of its digits is a prime.
(3) The sum of the squares of its digits is a prime.
(4) The number is a square.
(5) The sum of its digits is a square.
(6) The sum of the squares of its digits is a square.
(7) The number is triangular.
(8) The sum of its digits is triangular.
(9) The sum of the squares of its digits is triangular.

You must use them to determine the number."
"But they can't all be true!", interjected one of the students.
"I never said they were! Some of the statements are true and some are false."
"Well we will need more information. Tell us which are true and which are false."
"If I told you that you would easily be able to determine the number!"
"Well at least tell us how many are true."
"If I told you that now, you would be able to determine the number too easily!"
"Well, what more can you tell us?"
"Nothing! I have told you enough!"

What was the number?
A straightforward solution in Maple:
```filter := (S, pred) -> map(m -> `if`(pred(m), m, NULL), S):
flatten := LL -> map(op, LL):
issingleton := L -> evalb(nops(L) = 1):

classify := (L1, L2) -> {
seq(
[seq(
`if`(L2[i] = L2[j], L1[j], NULL),
j=1..nops(L1)
)],
i=1..nops(L1)
)
}: # inefficient!
classifyBy := (L, f) -> classify(L, map(f, L)):```
```> classify([1,2,3,4,5], [left, left, right, center, right]);
{, [1, 2], [3, 5]}

> classifyBy([1,2,3,4,5,6,7], isprime);
{[1, 4, 6], [2, 3, 5, 7]}```
```numDigits := n -> `if`(n < 10, 1, 1 + numDigits(iquo(n, 10))):

digitsum := L -> `+`(op(L)):
digitsquaresum := L -> `+`(op(map(n -> n^2, L))):
digitproduct := L -> `*`(op(L)):

istriangular := n -> type((sqrt(8 * n + 1) - 1) / 2, integer):

asNumber := L -> add(L[i] * 10^(nops(L) - i), i=1..nops(L)):

F := {
digitsum,
digitsquaresum,
digitproduct
}:

S := L -> flatten(
map(
[isprime, issqr, istriangular],
[asNumber, digitsum, digitsquaresum](L)
)
):

pos := combinat[permute]([seq(seq(j, i=1..4), j=1..9)], 4):
pos := filter(pos, L -> nops(map(numDigits, F(L))) = 3):

pos := classifyBy(pos, S):
pos := flatten(filter(pos, issingleton)):
pos := classifyBy([op(pos)], p -> convert(S(p), multiset)):

solution := filter(pos, issingleton):```
```> solution;
{[[3, 9, 6, 9]]}

> [F, S]([3,9,6,9]);
[{27, 207, 1458},
[false, true, false, false, false, false, false, false, false]]```