Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

share|improve this question
1  
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? –  Mr.Wizard Feb 21 '13 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. –  Tom De Vries Feb 21 '13 at 13:04
    
How are the students assumed to go about solving such a task? –  ssch Feb 21 '13 at 19:43
    
@ssch Trying all permutations by hand, obviously. It's called "sadism" :) –  belisarius Feb 21 '13 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".... –  Tom De Vries Feb 22 '13 at 0:03

1 Answer 1

up vote 10 down vote accepted

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

share|improve this answer
    
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! –  Tom De Vries Feb 21 '13 at 13:03
    
@TomDeVries The new code address your concerns –  belisarius Feb 21 '13 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!! –  Tom De Vries Feb 22 '13 at 0:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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