51
\$\begingroup\$

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!

\$\endgroup\$
12
  • 20
    \$\begingroup\$ 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. \$\endgroup\$
    – Dennis
    Commented Nov 26, 2014 at 3:37
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Nov 26, 2014 at 3:41
  • 5
    \$\begingroup\$ All whitespace required for the code to work is functional. \$\endgroup\$ Commented Nov 26, 2014 at 3:42
  • \$\begingroup\$ Okay, thanks for the response! \$\endgroup\$ Commented Nov 26, 2014 at 3:47
  • 1
    \$\begingroup\$ @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). \$\endgroup\$ Commented Nov 26, 2014 at 22:07

71 Answers 71

1 2
3
0
\$\begingroup\$

Javascript, 82 81 80 bytes

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

TI-BASIC, 29

Adapted from my answer to Generate Monday Numbers.

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

PHP, 44 bytes

function f($n,$s=str_split){
    foreach($s($n)as$d)$f|=!$d||$n%$d;return!$f;
}

Try it online.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Python 2: 36 38 Bytes

Excluding f=lambda n:

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

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

\$\endgroup\$
1
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Python 2, 53 bytes

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 43 bytes \$\endgroup\$
    – Jo King
    Commented Apr 29, 2020 at 8:52
-1
\$\begingroup\$

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}
\$\endgroup\$
3
  • 3
    \$\begingroup\$ How can you have -ish on a character count? \$\endgroup\$
    – Beta Decay
    Commented Dec 3, 2014 at 19:26
  • \$\begingroup\$ Converting to Community Wiki, as this is not your own work. \$\endgroup\$
    – Doorknob
    Commented Jul 8, 2015 at 11:43
  • \$\begingroup\$ 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. \$\endgroup\$ Commented May 2, 2019 at 8:54
-1
\$\begingroup\$

Scala, 44 bytes

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ error: recursive method f needs result type \$\endgroup\$
    – Jacob
    Commented Dec 3, 2014 at 19:00
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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