# 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>0;)n/=p%(n%10)>0?.1:10;return n<1;}
// 51 characters

// Breakdown:
// for(int p=n;         Saves one semicolon to put declaration into for loop
// n%10>0;)             Basic check-for-zero
// n/=                  Pretty simple, discarding one number off of n at a time
// p%(n%10)>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<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 -> true
12 -> true
120 -> false
122 -> true
13 -> false
32 -> false
22 -> true
42 -> false
212 -> true
213 -> false
162 -> true
204 -> 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!

• Welcome to PPCG! A few suggestions: 1. Not counting functional whitespace is a bad idea. Any answer written in Whitespace will automatically win. 2. Should our submission print/return true and false or are truthy/falsy values OK as well? 3. The java tag doesn't really apply here, as the challenge itself is unrelated to Java. Commented Nov 26, 2014 at 3:37
• Okay. sorry for the issues. Just to clear it up, would you consider the space in 'int p=n' to be functional, because I did not previously. I will fix the other issues you pointed out. Commented Nov 26, 2014 at 3:41
• All whitespace required for the code to work is functional. Commented Nov 26, 2014 at 3:42
• Okay, thanks for the response! Commented Nov 26, 2014 at 3:47
• @RickyDemer: since 0 would be an exceptional input in that case (it's the only number with 0 digits that is a multiple of each of them), I imagine most answers would just get longer in an uninteresting way to include a check for it. So I like the problem as posed by the title better (divisible by its digits, rather than being a multiple of its digits, which excludes 0). Commented Nov 26, 2014 at 22:07

# Javascript, 8281 80 bytes

a=prompt(q=1);b=a.split('');while(d=b.pop())q=q&!(a%d);alert(a.indexOf(0)+1?0:q)


# TI-BASIC, 29

Input X
int(10fPart(X10^(-randIntNoRep(1,1+int(log(X
not(max(remainder(X,Ans+2Xnot(Ans


To disqualify numbers that contain zeroes, I replace all zeroes with 2X, which will not divide X.

I don't count the Input X in the code size, because it is analogous to a method signature.

# Hassium, 130 Bytes

func i(n){m=n.toString();if(m.contains("0"))return false;foreach(d in m)if(n%d.toString().toDouble()!=0)return false;return true;}


Run and see expanded version here

# K (oK), 13 bytes

Solution:

f:{~+/(48!$x)!'x}  13 bytes excludes function definition (+4) per specification. Try it online! Examples: f 1024 0 f 128 1 f 333 1 f 334 0  Explanation: Evaluation is performed right-to-left. Convert (e.g.) 128 => 1 2 8. 128 mod each 1 2 8 => 0 0 0. Sum of this is 0. Not of this is 1: f:{~+/(48!$x)!'x} / the solution including function definition
f:{             } / assign lambda function to f
x  / implicit single parameter
!'   / mod (!) each-left with each-right
(     )     / do this together
$x / string x, 128 => "128" 48! / mod ascii values with 48, "128" => 1 2 8 +/ / sum over results ~ / not, 0 => 1, anything else => 0  # PHP, 44 bytes function f($n,$s=str_split){ foreach($s($n)as$d)$f|=!$d||$n%$d;return!\$f;
}


# Jelly, 3 bytes

ọDẠ


Try it online!

This is a link, i.e. a function in Jelly. Since Jelly doesn't have specific boolean values, this returns 1 for True and 0 for False.

# Python 2: 36 38 Bytes

Excluding f=lambda n:

f=lambda n:all([not n%(int(a)or n-1)for a inn])


Similar to the other python solution. Works because x or 0 returns x if x != 0.

Maybe some edge cases around 0-3 or something.

Edit: Didn't work, fixed it

# J, 18 Bytes (21 with function definition)

d=:0:=+/@:(|~"."0@":)


Explanation:

d=:                     | Define d
":    | Format into string
"."0       | Turn into array of digits
|~           | Starting Number mod each element
+/                | Sum
0:=                  | Test if it’s equal 0. Returns 1 for true, 0 for false


Other characters are for ensuring functions compose properly.

# Python 2, 53 bytes

lambda s:all(s%int(i)==0 if i!='0'else 0 for i ins)


Try it online!

• 43 bytes
– Jo King
Commented Apr 29, 2020 at 8:52

# Mathematica - 93ish characters

Robert G Wilson v gives us the following for Mathematica:

Function definition:

fQ[n_] :=
Block[{id = Union[IntegerDigits[n]]},
Union[IntegerQ[#] & /@ (n/id)] == {True}];


To call:

Select[Range[487], fQ[#] &]


Output (ignoring divide by zero complaints):

{1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22, 24, 33, 36, 44, 48, 55,
66, 77, 88, 99, 111, 112, 115, 122, 124, 126, 128, 132, 135, 144,
155, 162, 168, 175, 184, 212, 216, 222, 224, 244, 248, 264, 288, 312,
315, 324, 333, 336, 366, 384, 396, 412, 424, 432, 444, 448}

• How can you have -ish on a character count? Commented Dec 3, 2014 at 19:26
• Converting to Community Wiki, as this is not your own work. Commented Jul 8, 2015 at 11:43
• Oh, dear, the hallway monitor police are out. I gave full full credit, but I've broken some mysterious "rule" and so get castigated. This is precisely why I've given up even trying to contribute to StackExchange: aside from the privileged core who got in early, they just look down their noses at anyone new. Thanks for the -1. Commented May 2, 2019 at 8:54

# Scala, 44 bytes

def f(n:Int)=n+""forall{k=>k>48&&n%(k-48)<1}


Try it online!

• error: recursive method f needs result type Commented Dec 3, 2014 at 19:00