14
\$\begingroup\$

Task

Given two positive integers (dividend and divisor), calculate the quotient and the remainder.
Normally it would be calculated as e = o*q+r where q*o<=e and 0<=r<o.
For this challenge it still e = o*q+r but q*o>=e and -o<r<=0.
For example e=20 and o=3, normally it would be 20/3 -> 20=3*6+2, since 18<=20 and 0<=2<3. Here it will be 20/3 -> 20=3*7-1 where 21>=20 and -3<-1<=0

Test Cases

Input -> Output
20, 3 -> 7, -1
10, 5 -> 2, 0
7, 20 -> 1, -13
100, 13 -> 8, -4

You don't need to handle o=0.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Called it on being a trivial variant of regular divmod. \$\endgroup\$
    – Neil
    Dec 11, 2017 at 14:54
  • \$\begingroup\$ Is it acceptable to output r as the negation of the real r for languages that uses unsigned bytes to store data or assume overflowing? (-11 / 255) \$\endgroup\$
    – Uriel
    Dec 11, 2017 at 15:12
  • \$\begingroup\$ @Uriel yes, but add a note about this on the answer \$\endgroup\$
    – Rod
    Dec 11, 2017 at 15:14

25 Answers 25

8
\$\begingroup\$

Python 3, 39 26 bytes

Martin Ender saved 13 bytes

lambda x,y:(-(x//-y),x%-y)

Try it online!

Python 2, 25 bytes

lambda x,y:(-(x/-y),x%-y)

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ I think you can just do x%-y to get the remainder. \$\endgroup\$ Dec 11, 2017 at 12:54
  • \$\begingroup\$ Actually, why not go all the way... (-(x//-y),x%-y) \$\endgroup\$ Dec 11, 2017 at 12:55
  • \$\begingroup\$ @MartinEnder Thats really good \$\endgroup\$ Dec 11, 2017 at 12:57
  • \$\begingroup\$ @Mr.Xcoder Included both \$\endgroup\$ Dec 11, 2017 at 12:59
8
\$\begingroup\$

Jelly, 3 bytes

NdN

Try it online!

How it works

Abusing divmod again \o/. Look ma’ no unicode!

NdN - Full program / Dyadic chain. | Example: 7, 20

N   - Negate the first input.      | -7
 d  - Divmod by the second one.    | [-1, 13]
  N - Negate each again.           | [1, -13]
\$\endgroup\$
6
\$\begingroup\$

Haskell, 25 bytes

n#k=[-div(-n)k,mod n(-k)]

Try it online!

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

Mathematica, 21 bytes

{s=⌈#/#2⌉,#-#2s}&

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Can you add an explanation, please? \$\endgroup\$
    – Rod
    Dec 11, 2017 at 12:49
  • 2
    \$\begingroup\$ @Rod ⌈#/#2⌉ calculates the ceiling of their division, and stores it in a variable s, and then subtracts argument 2 * s from argument 1. \$\endgroup\$
    – Mr. Xcoder
    Dec 11, 2017 at 12:51
  • 1
    \$\begingroup\$ @Mr.Xcoder you are fast! \$\endgroup\$
    – ZaMoC
    Dec 11, 2017 at 12:55
5
\$\begingroup\$

05AB1E, 4 bytes

(s‰(

Try it online!

5 bytes

(‰ćÄJ

Try it online!

How they work

Abuses Python's modulo! \o/

(s‰(  | Full program. Let A and B be the two inputs. | Example: 100, 13.

(     | Compute -A.                                  | -100
 s    | Swap (reverse the stack, in this case).      | 13, -100
  ‰   | Divmod.                                      | [-8, 4]
   (  | Negative (multiply each by -1, basically).   | [8, -4]

----------------------------------------------------

(‰ćÄJ | Full program. Takes input in reverse order.

(     | Negative. Push -A.
 ‰    | Divmod
  ć   | Push head extracted divmod (make the stack [quotient, [remainder]].
   Ä  | Absolute value (operates on the quotient).
    J | Join the stack.
\$\endgroup\$
4
  • \$\begingroup\$ Ah yes, forgot that the divmod works with negative numbers :) \$\endgroup\$
    – Emigna
    Dec 11, 2017 at 13:09
  • \$\begingroup\$ And also, that's new functionality of J isn't it?. Never seen that before. Could definitely be useful. \$\endgroup\$
    – Emigna
    Dec 11, 2017 at 13:11
  • \$\begingroup\$ @Emigna It is described as Join. Push ''.join(a) if a is list; Else, push ''.join(stack). I think it is the new functionality, although I haven't ever used J before :P \$\endgroup\$
    – Mr. Xcoder
    Dec 11, 2017 at 13:13
  • \$\begingroup\$ It's definitely new. Tried on my local version from August and 5)6 gives ['5']6 :) \$\endgroup\$
    – Emigna
    Dec 11, 2017 at 13:17
4
\$\begingroup\$

Alice, 15 bytes

/O.
\io/R%e,R:R

Try it online!

Explanation

Ruby's integer division and modulo (on which Alice's are implemented) are defined such that using a negative divisor already sort of does what we want. If we negated the divisor we automatically get the correct modulo, and we get minus the quotient we want. So the easiest way to solve this is by negating a bunch of numbers:

/   Switch to Ordinal mode.
i   Read all input as a string "e o".
.   Duplicate the string.
/   Switch to Cardinal mode.
R   Implicitly convert the top string to the two integer values it
    contains and negate o.
%   Compute e%-o.
e,  Swap the remainder with the other copy of the input string. We can't
    use the usual ~ for swapping because that would convert the string 
    to the two numbers first and we'd swap e%-o in between e and o instead
    of to the bottom of the string.
R   Negate o again.
:   Compute e/-o.
R   Negate the result again.
\   Switch to Ordinal mode.
O   Output -(e/-o) with a trailing linefeed.
o   Output e%-o.

    The code now bounces through the code for a while, not doing much except
    printing a trailing linefeed when hitting O again. Eventually, the IP
    reaches : and attempts a division by zero which terminates the program.
\$\endgroup\$
4
\$\begingroup\$

Pari/GP, 18 bytes

x->y->-[-x\y,-x%y]

Try it online!

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

Julia, 18 bytes

x$y=.-fldmod(-x,y)

Try it online!

.- is element wise negation, and fldmod returns a tuple made of the results of floored divison and corresponding residue.

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

MATL, 5 4 bytes

_&\_

Try it online!

-1 byte thanks to Luis Mendo

      # implicit input
_     # unary minus (negates first input, o)
&\    # alternative output mod, returns remainder, quotient, implicitly takes e
_     # unary minus, takes the opposite of the quotient.
      # implicit output, prints stack as remainder
                                         quotient

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

J, 16 bytes

([-]*a),~a=.>.@%

This is essentially the Jenny_mathy's Mathematica solution rewritten in J.

How it works:

a=.>.@% Finds the ceiling of the division of the left and right arguments and stores it into variable a

,~ concatenated to (reversed)

([-]*a) subtracts a*right argument from the left argument

Try it online!

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

R, 31 29 bytes

-2 bytes thanks to Giuseppe

function(e,o)-c(e%/%-o,-e%%o)

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I think you can do a 29 byter with -c(e%/%-o,-e%%o) \$\endgroup\$
    – Giuseppe
    Dec 11, 2017 at 14:35
2
\$\begingroup\$

Common Lisp, 7 bytes

Built-in function ceiling returns two values: the ceiling of the quotient, and the remainder to match:

$ clisp -q
[1]> (ceiling 20 7)
3 ;
-1
\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 37 31 29 27 25 bytes

Saved 2 bytes thanks to @Rod
Saved 2 bytes thanks to @ETHproductions

Takes input in currying syntax. Returns [q,r].

a=>b=>[q=~-a/b+1|0,a-q*b]

Test cases

let f =

a=>b=>[q=~-a/b+1|0,a-q*b]

console.log(JSON.stringify(f(20)(3)))   // -> 7, -1
console.log(JSON.stringify(f(10)(5)))   // -> 2, 0
console.log(JSON.stringify(f(7)(20)))   // -> 1, -13
console.log(JSON.stringify(f(100)(13))) // -> 8, -4

\$\endgroup\$
2
  • \$\begingroup\$ You can probably q=(a+b-1)/b+|0 instead q=a/b+.9|0 \$\endgroup\$
    – Rod
    Dec 11, 2017 at 13:01
  • \$\begingroup\$ @ETHproductions Sounds like a plan. ;) \$\endgroup\$
    – Arnauld
    Dec 12, 2017 at 7:30
1
\$\begingroup\$

Perl 5, 30 + 1 (-p) = 31 bytes

say+($_-($\=$_%-($"=<>)))/$"}{

Try it online!

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

4, 55 50 bytes

3.711712114001231311141130013513213131211513115154

Try it online!

Represents the reminder by it's negation (10 instead of -10), since the language uses byte input and output, deemed valid by OP comment.

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

Commentator, 90 bytes

//
;{- -}
{-{-//-}e#<!-}
;{-{-{- -}-}-}
{-{-{-e#-}
;{-{- -}-}
{-%e#*/-}#          /*-}e#*/

Try it online!

Outputs the remainder, then the quotient, newline separated.

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

C (gcc), 43 bytes

f(x,y,z)int*x;{for(z=0;*x>0;*x-=y)z++;y=z;}

Usage

main(){
    int a=7,b=20,c; 
    c=f(&a,b); 
    printf("%d %d\n",c,a);
}

Try it online!

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

Java (OpenJDK 8), 30 bytes

(i,j)->(i+j-1)/j+","+(i%j-j)%j

Try it online!

7,-1
2,0
1,-13
8,-4
\$\endgroup\$
0
\$\begingroup\$

Add++, 35 bytes

L*,@0$_$2D2D%V/1B]b\GLVB]-1Gx$pBcB*

Try it online!

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

C (gcc) 41 bytes

f(a,b){b=(a+b-1)/b;}g(a,b){b=a-f(a,b)*b;}

This may be cheating, using two functions and it may fail other tests?

Try it online

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

Swift, 47 bytes

func f(a:Int,b:Int){print((a+b-1)/b,(a%b-b)%b)}
\$\endgroup\$
0
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 124 123 105 bytes

 E =INPUT
 O =INPUT
 Q =E / O
 R =E - Q * O
 EQ(0,R) :S(A)
 R =R - O
 Q =Q + 1
A OUTPUT =Q
 OUTPUT =R
END

Try it online!

Takes input as E, then O, separated by a newline and prints out Q, then R, separated by a newline.

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

TXR: 8 bytes

Built-in function ceil-rem. E.g. (ceil-rem 20 7) yields (7 -1).

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

Clean, 42 bytes

import StdEnv
f a b#c=(a-1)/b+1
=(c,a-c*b)

Try it online!

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

Deorst, 23 bytes

@l0-%z]@l0-,l0-@l0-miE_

Try it online!

How it works

@                       - Swap the inputs
 l0-                    - Negate
    %                   - Modulo
     z]                 - Push the inputs
       @                - Swap
        l0-             - Negate
           ,            - Integer divide
            l0-         - Negate
               @        - Swap
                l0-     - Negate
                   miE_ - Print
\$\endgroup\$

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.