Counting numbers that are divisible by 3 and can be produced from the digits of a given number
The algorithm can be improved, so that we do less work:

Use the fact that multiples of 3 have a sum of digits that's also a multiple of 3.  That means we can select all the distinct combinations of digits, and discard any that don't sum to a multiple of 3.  Only for those sets not discarded, we can compute the number of permutation of the selected digits.  (Note that we don't have to actually produce all the numbers, merely count them - so if we have n different digits, we count n! permutations; if p of them are identical, then n!/p!, and so on).

For example, with input 5532:


5+5+3+2 is 15, this gives us 4!/2! permutations that are multiples of 3 (and remember 5532 as the largest such multiple).
5+5+3 is 13, so ignore it.
5+5+2 is 12, giving us 3!/2! more permutations to add to our count.
5+3+2 is 10; ignore it.
5+5 is 10; ignore it.
5+3 is 8; ignore it.
5+2 is 7; ignore it.
2+3 is 5; ignore it.
5 alone - ignore
3 alone - add one to the count.
2 alone - ignore.


And we're done; the total count is 4!/2! + 3!/2! + 1 = 16.