2
$\begingroup$

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

$\endgroup$
3
$\begingroup$

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][[1]]; Mod[n, z],
    2, "Impossible",
    3, "Impossible",
    _, "Don't know what to do since Total[restQ] == " <> IntegerString[counter] <> "."
    ]
   ]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Yes this did help, thank you! $\endgroup$ – Kenneth Eaves Feb 17 '18 at 22:26
  • $\begingroup$ Glad to hear that! You're welcome! $\endgroup$ – Henrik Schumacher Feb 18 '18 at 0:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.