Is a number divisible by each of its digits? My friend and I were working on a lab in our AP Computer Science class and decided to code golf one one the problems since we still had half the class free after we finished. Here is the question:


  Given a number n, is n divisible by each of its digits?


For example, 128 will pass this test- it is divisible by 1,2, and 8. Any numbers with a zero automatically disqualify the number. While you may use other languages and post solutions with them if you like, we are most interested in seeing how compact people can make the program in Java, as that is the language we use in the class. So far, we both have 51. Here is my current code:

public boolean dividesSelf(int n){for(int p=n;n%10&gt;0;)n/=p%(n%10)&gt;0?.1:10;return n&lt;1;}
// 51 characters

// Breakdown:
// for(int p=n;         Saves one semicolon to put declaration into for loop
// n%10&gt;0;)             Basic check-for-zero
// n/=                  Pretty simple, discarding one number off of n at a time
// p%(n%10)&gt;0?          If p (the given value) is not divisible by n%10 (the current digit)...
// .1:10;               Divide by .1 (multiply by 10) so it fails the check next iteration. If it is divisible, divide by 10 to truncate the last digit
// return n&lt;1           If the number was fully divisible, every digit would be truncated, and n would be 0. Else, there would still be non-zero digits.


Requirements

The method signature can be whatever you want. Just count the function body. Make sure, though, that the method returns a boolean value and only passes in one numeric parameter (not a string).

The code must be able to pass all of these cases (in order to stay true to the directions of the original question, only boolean true and false values count if the language supports booleans. If and only if your language does not have boolean variables you may represent false with 0 and true with any nonzero integer (preferably 1 or -1):

128 -&gt; true
 12 -&gt; true
120 -&gt; false
122 -&gt; true
 13 -&gt; false
 32 -&gt; false
 22 -&gt; true
 42 -&gt; false
212 -&gt; true
213 -&gt; false
162 -&gt; true
204 -&gt; false


Also, we didn't count whitespace, so feel free to do the same, unless the whitespace is essential to the working of the program (so newlines in Java don't count, but a single space between int and x=1 does count.)
Good luck!
Perl 6, 13

sub golf($_) {
   $_%%.comb.all
}


Uses the implicit variable $_ — $_ %% .comb.all is equivalent to $_ %% all($_.comb). %% is the "is divisible" operator, and comb with no additional argument returns a list of the characters in a string. As an example, if the argument is 123, then the function evaluates

123 %% all(123.comb)


which is

123 %% all(1, 2, 3)


junction autothreading makes it

all(123 %% 1, 123 %% 2, 123 %% 3)


which is

all(True, False, True)


which is false in boolean context because it's an "all" junction and clearly not all of its elements are true.

It should be possible to coerce the return value to Bool and hide the junction-ness from callers by making the function signature sub golf($_ --&gt; Bool()), but coercions in function signatures don't work yet in Rakudo. The return value is still correctly true or false, it's just not True or False.