# Conditional Programming

I need help creating a function that takes 5 integer inputs and does the following:

If exactly 2 or 3 of the first four inputs are not divisible by the last input I want the output to read “Impossible”

If exactly 1 of the first four inputs (let’s call it Z) is not divisible by the last input, then I want the output to be the smallest integer congruent to the last input modulo Z.

If all the first four integers are divisible by the last integer, I want the output to be the sum the first four digits.

I’m trying to teach myself Mathematica, and need a lot of help...

This should get you started:

f = {i1, i2, i3, i4, n} \[Function] Module[{list, restQ, z, counter},
list = {i1, i2, i3, i4};
restQ = Unitize[Mod[list, n]];
counter = Total[restQ];
Switch[counter,
0, Total[list],
1, z = Pick[list, restQ, 1][]; Mod[n, z],
2, "Impossible",
3, "Impossible",
_, "Don't know what to do since Total[restQ] == " <> IntegerString[counter] <> "."
]
]

• Yes this did help, thank you! – Kenneth Eaves Feb 17 '18 at 22:26
• Glad to hear that! You're welcome! – Henrik Schumacher Feb 18 '18 at 0:58