# Calculate the Upper Divmod

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.

• Called it on being a trivial variant of regular divmod.
– Neil
Dec 11 '17 at 14:54
• 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) Dec 11 '17 at 15:12
– Rod
Dec 11 '17 at 15:14

# 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!

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

# 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]


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


Try it online!

# Mathematica, 21 bytes

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


Try it online!

– Rod
Dec 11 '17 at 12:49
• @Rod ⌈#/#2⌉ calculates the ceiling of their division, and stores it in a variable s, and then subtracts argument 2 * s from argument 1. Dec 11 '17 at 12:51
• @Mr.Xcoder you are fast! Dec 11 '17 at 12:55

# 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.

• Ah yes, forgot that the divmod works with negative numbers :) Dec 11 '17 at 13:09
• And also, that's new functionality of J isn't it?. Never seen that before. Could definitely be useful. Dec 11 '17 at 13:11
• @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 Dec 11 '17 at 13:13
• It's definitely new. Tried on my local version from August and 5)6 gives ['5']6 :) Dec 11 '17 at 13:17

## 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.


# Pari/GP, 18 bytes

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


Try it online!

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. # 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  # 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! # R, 31 29 bytes -2 bytes thanks to Giuseppe function(e,o)-c(e%/%-o,-e%%o)  Try it online! • I think you can do a 29 byter with -c(e%/%-o,-e%%o) Dec 11 '17 at 14:35 # 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


# JavaScript (ES6), 37312927 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

• You can probably q=(a+b-1)/b+|0 instead q=a/b+.9|0
– Rod
Dec 11 '17 at 13:01
• @ETHproductions Sounds like a plan. ;) Dec 12 '17 at 7:30

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

say+($_-($\=$_%-($"=<>)))/$"}{  Try it online! # 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. # Commentator, 90 bytes // ;{- -} {-{-//-}e#<!-} ;{-{-{- -}-}-} {-{-{-e#-} ;{-{- -}-} {-%e#*/-}# /*-}e#*/  Try it online! Outputs the remainder, then the quotient, newline separated. # 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! # 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  # Add++, 35 bytes L*,@0$_$2D2D%V/1B]b\GLVB]-1Gx$pBcB*


Try it online!

# 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

# Swift, 47 bytes

func f(a:Int,b:Int){print((a+b-1)/b,(a%b-b)%b)}


# SNOBOL4 (CSNOBOL4), 124123 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.

# TXR: 8 bytes

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

# Clean, 42 bytes

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


Try it online!

# 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