Find if there exist some combination of these digits that will be divisible by 8 or not

595 Views Asked by At

Let's say I am given some 100 digits and I have to find whether there can be any combination of these digits such that the number formed will be divisible by 8, how can I do that?

I know divisibility rule for 8 is that any number will be divisible by 8 if last 3 digits are divisible by 8.. But how to find that in a shuffled digits?

1

There are 1 best solutions below

4
On BEST ANSWER

The ones digit has to be even, which will make the final number divisible by $2$. If the ones digit is $2$ or $6$, the tens digit needs to be odd so the final number is divisible by $4$. If the ones digit is $0,4,8$ the tens digit needs to be even. Then pick the hundreds digit (you can find a similar rule) so the whole thing is divisible by $8$

Of course, if there is an $8$ in the pile, you can just pick that and stop.

Added: another approach is to make a list of all the multiples of $8$ from $000$ through $992$. Then just look through you list of digits to see if you have any combination. If you have $100$ digits, the odds you have any given one is quite high, so you won't have to look far.