# Divisibility test

Given two strictly positive integers n and d as input, determine whether n is evenly divisible by d, i.e., if there exists an integer q such that n = qd.

You may write a program or a function and use any of the our standard methods of receiving input and providing output.

The output should be a truthy or a falsy value; truthy if n is divisible by d, and falsy otherwise.

Your code only has to handle integers it can represent natively, as long as it works for all signed 8-bit integers. However, your algorithm has to work for arbitrarily large integers.

You may use any programming language, but note that these loopholes are forbidden by default.

This is , so the shortest valid answer – measured in bytes – wins.

### Test cases

n,  d    output

1,  1    truthy
2,  1    truthy
6,  3    truthy
17, 17    truthy
22,  2    truthy
1,  2    falsy
2,  3    falsy
2,  4    falsy
3,  9    falsy
15, 16    falsy

The Stack Snippet at the bottom of this post generates the catalog from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

## Language Name, N bytes

where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes

If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 3 (-p flag) = 45 bytes

You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes

• This conversation has been moved to chat. – Dennis Jul 21 '16 at 19:12

# Clojure, 15 bytes

#(=(mod % %2)0)

## dc, 8 7 bytes

Input is delimited by a space: n d

?~/z1-n

If false, it outputs 0. If true, it outputs 1 and throws an error about division by zero.

Explanation:

?   # Take input from stdin.
~   # Pop two values from stack. Push quotient. Push remainder.
/   # Attempt to divide quotient by remainder.
#   If input is divisible, then remainder is 0.
#     Division fails, throwing an error and leaving both numbers on stack.
#     (Stack depth is 2.)
#   If input is not divisible, then remainder is not 0.
#     Division succeeds, and result is pushed on stack. (Stack depth is 1.)
z   # Push stack depth on stack. (If divisible, push 2; if indivisible, push 1.)
1-  # Subtract 1 from top of stack. ToS is now 1 or 0.
n   # Pop top of stack and print it as a number.
• Ironically, this reports that all numbers are divisible by 0. – Joe Jul 23 '16 at 0:42
• I came back and re-read this answer and it totally confused me. "Why was I dividing the quotient? What was I thinking?" I just realized, it doesn't matter what the quotient is, or what the second quotient is. What matters is whether the division is possible—if the ~ gave remainder 0, then division is not possible again, which is what differentiates a true case from a false one. – Joe Oct 4 '16 at 6:47

# Sesos, 16 bytes

Hexdump:

0000000: d6659c af71e7 a0fbf8 70cedc ae8de7 1e             .e..q....p......

Try it online!

Assembler:

set numin
set numout
get,fwd 1,get,rwd 1
jmp
fwd 1,sub 1,fwd 1,add 1,rwd 1
jmp
fwd 2
jnz
fwd 1
jmp
jnz
rwd 3
jmp
rwd 1
jnz
fwd 1
sub 1
jnz
jmp
fwd 1
jnz
fwd 2
put

Brainfuck: ,>,<[>->+<[>>]>[-<+>]<<<[<]>-]>>>>+<<[>]>>.

## ROOP, 24 bytes

I
w
w
R #H
#
N
#
W
O#

The I is the input object. When the object is on the operator w wait the entry of a number that puts it under. Then the I moves to the right and falls on the second w waiting for the second number. The operator R removes those two numbers, and make the remainder of divide them below. The N operator removes that number and creates a 1 if the number was 0, and 0 otherwise. Then the W operator puts that number in the O object representing the output. At the same time the I reached the operator H that ends execution.

# Batch, 27 bytes

for /l %a IN (1,1,10)DO @%a

Does ' is not recognized as an internal or external command, operable program or batch file.' count as a separator?

## Labyrinth, 8 bytes

??
@%
!1

Input is just the two numbers, using any non-numeric separator of your choice. Output is either 1 for truthy or nothing at all for falsy.

Try it online!

Alternative solution that prints 0 for falsy but terminates with an error (same byte count):

<1%??
!;

### Explanation

There's only one branch in the execution and that's after the modulo (%). When the input is a truthy case, the following is executed:

?   Read integer and push onto stack.
?   Read integer and push onto stack.
%   Take the first modulo the second integer. The result is zero, so the
instruction pointer keeps moving south.
1   Turn that zero into a one.
!   Print it.
@   Terminate the program.

Otherwise, the following code is executed:

?   Read integer and push onto stack.
?   Read integer and push onto stack.
%   Take the first modulo the second integer. The result is positive, so
the instruction pointer turns west.
@   Terminate the program.

# Java, 13 bytes

(a,b)->a%b<1;

This is a java.util.function.BiPredicate<Integer, Integer>.

As something that makes more sense to those who are new to Java, it takes up 37 bytes:

boolean A(int b,int B){return b%B<1;}

As something that compiles, it takes up 46 bytes:

class a{boolean A(int b,int B){return b%B<1;}}

As something that runs, it takes up 104 bytes:

interface a{static void main(String[]A){System.out.print(Integer.decode(A[0])%Integer.decode(A[1])<1);}}

For the sake of completeness, here's a 50-byte lambda that checks if an arbitrarily large integer a is divisible by another arbitrarily large integer b. It's a BiPredicate<BigInteger, BigInteger>.

(a,b)->a.mod(b).equals(java.math.BigInteger.ZERO);
• I'm quite sure that there's this exact answer somwhere here... – Leaky Nun Jul 23 '16 at 16:40
• @LeakyNun Well, in fact this is semantically different than the answer you're looking for - David's answer nests two lambdas, while mine uses a single lambda with two parameters. They just exploit the identical rule which states that if a is divisible by b then a mod b is 0. – dorukayhan Jul 23 '16 at 16:48

# Logicode, 289 262 bytes

Presenting the language that's more verbose than Java!

circ d(n)->cond n<->0+n/d(n>)
circ e(n)->[
cond n->var a=~((~(d(n)))>)/var a=0
cond (~n)<->var b=a+0/var b=e(a)+1
b
]
circ f(a,b)->cond *a&*b->f(e(a),e(b))/a
circ g(a,b)->!(*(f(b,a)))
circ h(a,b)->cond b->h(e(a),e(b))/a
circ i(a,b)->cond g(a,b)->i(h(a,b),b)/c(a)

I'll post an explanation later, but it's basically a shortened version of my prime checker.

Added a new feature: * (boolean)!

# Vitsy, 1 Byte

This is a function that leaves 0 on the stack if true and a non-zero integer on the stack if false.

M

(This is the modulo function.)

Try it Online!

(N has been added for output).

• Is 0 truthy in Vitsy? – Dennis Oct 5 '16 at 17:29
• @Dennis: Technically -1 < x < 1 is truthy because of how I've set up the ( (if) command. – Addison Crump Oct 5 '16 at 17:30
• Huh. A bit unconventional, but certainly interesting. – Dennis Oct 5 '16 at 17:33

## Emotinomicon, 16

😼😼😌😨

Explanation:

😼😼😌😨
😼        pushes integer input
😼      pushes integer input
😌    pops n,m; pushes n mod m
😨  pops n; outputs as number

Returns zero for truthy if first integer is divisible by second, returns non-zero for falsy.

# 05AB1E, 2 1 byte

Ö

Try it online!

Was (Test here) but arguments switched (suggested by @Mego). This allowed me to golf down to 1 byte.

Explanation (old):

s    Reverses input e.g. 6, 3 -> 3,6 so that input is in correct order
Ö   Checks if (top of stack % second top of stack) == 0 e.g. 6 % 3 == 0
Implicitly prints (1 because 6 % 3 = 0)
• Reversing the stack isn't necessary - you can just take the input in the opposite order. – user45941 Oct 7 '16 at 17:24
• Are you sure the inputs can be taken in that order according to the rules? If they can, I will switch to just Ö. @Mego – Geno Racklin Asher Oct 7 '16 at 18:38

## Racket 26 bytes

(λ(n m)(= 0(modulo n m)))

Usage:

(define f
(λ(n m)
(= 0
(modulo n m))))

Testing:

(f 1 1)
(f 2 1)
(f 6 3)
(f 17 17)
(f 22 2)
(f 1 2)
(f 2 3)
(f 2 4)
(f 3 9)
(f 15 16)

Output:

#t
#t
#t
#t
#t
#f
#f
#f
#f
#f

## tinylisp (REPL), 38 bytes

(d |(q((N D)(i(l N D)(e N 0)(|(s N D)D

Defines a function | that takes N and D and returns 1 if divisible, 0 otherwise. The REPL infers closing parentheses as necessary at the end of the code. Call the function like (| 22 2).

Ungolfed/explanation:

(d |                  Define | to be...
(q (                 a function, i.e. a list containing...
(N D)               list of params N and D, and function body:
(i (l N D)           If N is less than D, then return:
(e N 0)              1 if N equals 0, 0 otherwise;
(| (s N D) D)))))    Else, recurse with arguments N - D and D

Gets kinda slow for N around D * 10^5 and larger.

# TI-Basic, 10 bytes

Prompt N,D:not(fPart(N,D

Returns 1 for true or 0 for false

# GameMaker Language, 32 bytes

return 1>argument0 mod argument1

## bc, 19 bytes

me@LCARS:/PPCG$bc --quiet divisibility_test.bc 12 4 1 me@LCARS:/PPCG$ bc --quiet divisibility_test.bc