# Creating a number based on given conditions

In the grade 7 math curriculum here in Alberta we teach divisibility. I'm trying to write a program to create questions based on the idea of a student being given say, 4 digits, and then choosing one of their own to create say, "the largest number divisible by 5".

I have a few simple ideas. Get a list of 4 numbers...

RandomInteger[{0, 9}, 4]


Arrange them

Permutations[list]


Turn them into numbers...

FromDigits /@ Permutations[list]


I'm sure there must be an elegant way to use Mathematica to do this. I'd need to have the original 4 digits and the answer so I could create the question under program control.

I.E. Given the digits 9, 0, 0, 2 and 1 other digit of your choice, create the largest 5 digit number you can that is divisible by 5.

I'd appreciate any hints or suggestions.

• That doesn't look like a bad start. Have you tried writing it like that yourself? What problems did you run into, or where was the implementation lacking? Commented Feb 21, 2013 at 11:52
• Hi, I could find the maximum given a list, but didn't know how to "elegantly" pick the extra digits , create the possible numbers, and find the largest. Often the case with my skills, I'm learning but often need a bit of help. Getting help here is such a great source of assistance. Commented Feb 21, 2013 at 13:04
• How are the students assumed to go about solving such a task?
– ssch
Commented Feb 21, 2013 at 19:43
• @ssch Trying all permutations by hand, obviously. It's called "sadism" :) Commented Feb 21, 2013 at 21:59
• Well, they would all be VERY SIMPLE problems, I.E. a number divisible by 2, 5, 10, 9, 3. Just those that have nice divisibility tests... so, hopefully it's not sadism!! It's a "standard" problem for this topic, that doesn't make it right, but it's what I have to "teach".... Commented Feb 22, 2013 at 0:03

Something like this?

f[initialNbrOfdigits_, extradigits_, divisor_] := Module[{sol = -Infinity, s},
While[sol == -Infinity,
s   = RandomInteger[{0, 9}, initialNbrOfdigits];
sol = Max@Select[FromDigits /@  Flatten[Permutations[Join[s, #]] & /@
Tuples[Range[0, 9], {extradigits}], 1], Divisible[#, divisor] &]];
Column@{Row[{"Given the digits ", s, " and ", extradigits,
" more of your choice, create the largest ", extradigits + initialNbrOfdigits,
" digit number you can that is divisible by ", divisor}],
Row[{"Solution: ", sol}]}
];
f[3, 2, 773]


Given the digits {9,9,4} and 2 more of your choice, create the largest 5 digit number you can that is divisible by 773

Solution: 98944

Edit

With a small modification to avoid considering numbers with leading zeroes, you could ask also for the minimum:

f[initialNbrOfdigits_, extradigits_, divisor_, f_] :=
Module[{sol = -Infinity, s},
While[sol == -Infinity,
s = RandomInteger[{0, 9}, initialNbrOfdigits];
sol = f@Select[FromDigits /@ Flatten[Permutations[Join[s, #]] & /@
Tuples[Range[0, 9], {extradigits}], 1], Divisible[#, divisor] && # >=
10^(extradigits + initialNbrOfdigits - 1) &]];
Column@{Row[{"Given the digits ", s, " and ", extradigits,
" more of your choice, create the " , f, " ", extradigits + initialNbrOfdigits,
" digit number you can that is divisible by ", divisor}],
Row[{"Solution: ", sol}]}];

f[3, 2, 18, Min]


Given the digits {8,2,2} and 2 more of your choice, create the Min 5 digit number you can that is divisible by 18
Solution: 12258

• Thanks! That looks brilliant. I'll see if I get any other answers but I can use this right now. I know it probably wouldn't happen often, but how would I check to make sure I actually get a number? I.E. how do I check list s to insure it has at least 1 non-zero digit? Thanks for your help, that last line is so compact, I'll have to go through carefully to see what's happening, but that's definitely what I needed! Commented Feb 21, 2013 at 13:03
• @TomDeVries The new code address your concerns Commented Feb 21, 2013 at 14:43
• Thanks again, very useful, and I'll use the "minimum" question as well. Already used this today to create a set of questions for a student. Much appreciated!! Commented Feb 22, 2013 at 0:05