-12
\$\begingroup\$

Challenge

Given two numbers, output their quotient. In other words, integer divide one number by another.

Both divisor/dividend will be under 10001. Division must be performed using integer division, rounding down.

Here are some example inputs and outputs:

5       1       5
-4      2       -2
4       -2      -2
6       2       3
16      4       4
36      9       4
15      2       7
17      3       5
43      5       8
500     5       100
500     100     5
10000   2       5000

Or as CSV:

5,1,5
-4,2,-2
4,-2,-2
6,2,3
16,4,4
36,9,4
15,2,7
17,3,5
43,5,8
500,5,100
500,100,5
10000,2,5000

Rules

  • Standard loopholes not allowed
  • You must use integer division, not floating point division
  • This is . Shortest answer wins, but will not be selected.

  • The second input (denominator) will never be equal to 0.

Why?

I'm interested in seeing answers in esoteric languages, like Brainfuck and Retina. Answers in regular languages will be trivial, however I would really like to see answers in these languages.

I will be giving a bounty of +50 reputation to a Retina answer.

\$\endgroup\$
19
  • 4
    \$\begingroup\$ Closely related: Division and remainder \$\endgroup\$
    – Luis Mendo
    Commented Mar 31, 2017 at 19:54
  • 3
    \$\begingroup\$ Do we need to handle negative numbers? None of the test cases are negative, but the question only specifies denominator != 0. \$\endgroup\$
    – DLosc
    Commented Mar 31, 2017 at 20:14
  • 9
    \$\begingroup\$ You just invalidated almost all the interesting approaches... \$\endgroup\$ Commented Mar 31, 2017 at 20:33
  • 4
    \$\begingroup\$ -1 Challenge is lame when negatives are required. \$\endgroup\$
    – mbomb007
    Commented Mar 31, 2017 at 20:36
  • 11
    \$\begingroup\$ I can't tell you the reason for the other downvotes, but mine is because I firmly believe that trivial questions like this are bad for the site. \$\endgroup\$ Commented Mar 31, 2017 at 20:45

23 Answers 23

3
\$\begingroup\$

Mathematica, 8 bytes

Quotient

Thanks to @JungHwanMin for reminding me there's a builtin.

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

JavaScript, 11 bytes

JavaScript doesn't have integers but using a bitwise or parses a float as a signed 32 bit integer.

a=>b=>a/b|0

Try it online

const f = a=>b=>a/b|0
alert(f(prompt())(prompt()))

\$\endgroup\$
2
\$\begingroup\$

C (GCC), 13 bytes

Doesn't work on all implementations, but that's OK.

f(a,b){a/=b;}

Try it on TIO!

\$\endgroup\$
2
\$\begingroup\$

Pip, 4 bytes

a//b

Try it online!

Explanation:

      A and B are read implicitly from the cmd line
a//b  Calculate the int div of a and b (double slash == int div)
      Results of the last expression are printed implicitly.
\$\endgroup\$
2
\$\begingroup\$

CJam, 3 bytes

q~/

Try it online!

\$\endgroup\$
2
\$\begingroup\$

아희(Aheui), 15 bytes

방방나망희

Try it here! (please copy and paste the code manually)

\$\endgroup\$
2
\$\begingroup\$

Python 2, 14 11 bytes

int.__div__

Try it online

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

Bash, 25 22 15 bytes

echo $(($1/$2))

Save to script, input numbers given at command line

Saved 3 bytes thanks to @betseg

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Programming Puzzles and Code Golf StackExchange! Nice first answer. Also, you can remove the space after the semicolon and the spaces inside parentheses. \$\endgroup\$
    – betseg
    Commented Mar 31, 2017 at 20:48
2
\$\begingroup\$

QBIC, 8 bytes

::?a'\`b

Explanation:

::    get a and b from the cmd line
?     Print
a \ b the backslash does integer division, (opposed to the forward slash / for float div)
  ' ` however, the same symbol is used for ELSE. We need to escape it with ' and `

Note that the latest version of QBIC allows for an inline use of the : function, saving two bytes:

?:'\`:
\$\endgroup\$
2
\$\begingroup\$

Aceto, 6 bytes

riri/p
ri takes input as integer
/ does integer division
p prints it

Try it online!

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

PHP, 23 Bytes

<?=$argv[1]/$argv[2]^0;

or

<?=$argv[1]/$argv[2]|0;

and for 29 Bytes

<?=intdiv($argv[1],$argv[2]);
\$\endgroup\$
1
\$\begingroup\$

Jelly, 1 byte

:

Try it online!

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

05AB1E, 3 bytes

II÷

Try online

If you can reverse the input, it would work in 1 byte: ÷

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

Triangular, 6 bytes

$.$%_<

Try it online!

Formats into this triangle:

  $
 . $
% _ <

Without directionals and no-ops, the code looks like this: $$_%

  • $ - read input as integer
  • _ - divide
  • % - print
\$\endgroup\$
1
\$\begingroup\$

Decimal, 12 bytes

81D81D44D301

Ungolfed:

81D   ; builtin 1 - read INT to stack
81D   ; builtin 1 - read INT to stack
44D   ; math divide (postfix /)
301   ; print from stack to output

Try it online!

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

Retina, 28 bytes

\d+
*
~`(_+) (_+)
K`$1$nC`$2

Try it online!

I'm quite new to Retina; I probably could have golfed this more.

Explanation:

Let's start with an input: 38 4

\d+
*

This turns the numbers into unary, so 4 is represented as ____, and 38 with 38 underscores.

~`...

This marks a stage that generates code to run afterwards

(_+) (_+)
K`$1$nC`$2

This is a replace stage to generate the division code; Firstly, it splits the string into the dividend (referenced as $1) and divisor (referenced as $2). Then, it returns the string:

K`{dividend as unary}
C`{divisor as unary}

The returned string is evaluated and the answer implicitly printed. It sets the working string to the dividend, and counts the matches of the divisor in it.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 20 bytes after golfing: Try it online! \$\endgroup\$
    – Neil
    Commented Mar 7, 2021 at 12:01
  • \$\begingroup\$ Nice! I'd completely forgotten about this but that's an interesting golf \$\endgroup\$
    – lolad
    Commented Mar 7, 2021 at 16:28
1
\$\begingroup\$

Keg, 3 bytes

¿¿/

Simply 2 nice inputs and then divides them.

TIO

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

Batch, 16 bytes

@cmd/cset/a%1/%2

set/a doesn't print its output inside a batch script, so we have to fake it out by starting a new copy of the command processor.

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

GML, 30 bytes

return argument0 div argument1
\$\endgroup\$
0
\$\begingroup\$

TI-Basic, 6 bytes

Input :iPart(X/Y
\$\endgroup\$
0
\$\begingroup\$

Retina, 40 38 bytes

\d+
*
(-?)(_+);(-?)(\2)*_*
$1$3$#4
--

Takes input in the format denominator;numerator. Also works for negative integers after the specs got changed, unlike the existing Retina answer.

-2 bytes thanks to @FryAmTheEggman thanks to a golf suggested in another Retina answer of mine.

Try it online.

Explanation:

Convert all numbers in the input to unary, replacing them with that amount of underscores:

\d+
*

Integer divide the two unary numbers (but with a decimal numbers as result), and filter all - to the front at the same time:

(-?)(_+);(-?)(\2)*_*
$1$3$#4

Remove any -- if present:

--

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

J-uby, 2 bytes

🫤

:/

Attempt This Online!

\$\endgroup\$
-1
\$\begingroup\$

Ruby, 10 bytes

->i,j{i/j}

Extremely simple. Lambda function that returns the value i/j.

\$\endgroup\$
2
  • 5
    \$\begingroup\$ -1, if an answer is extremely short and boring with just the code, you should add a link, or an explanation, or a comment on the code or something other than My answer's really boring, so blah blah blah. \$\endgroup\$
    – DJMcMayhem
    Commented Mar 31, 2017 at 20:25
  • \$\begingroup\$ @DJMcMayhem Fixed \$\endgroup\$
    – anna328p
    Commented Mar 31, 2017 at 20:31

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.