Calculate the Upper Divmod

13

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.

Rod

Posted 2017-12-11T12:17:13.587

Reputation: 17 588

3Called it on being a trivial variant of regular divmod. – Neil – 2017-12-11T14:54:36.340

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) – Uriel – 2017-12-11T15:12:59.267

@Uriel yes, but add a note about this on the answer – Rod – 2017-12-11T15:14:54.917

Answers

8

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!

Halvard Hummel

Posted 2017-12-11T12:17:13.587

Reputation: 3 131

I think you can just do x%-y to get the remainder. – Martin Ender – 2017-12-11T12:54:44.787

Actually, why not go all the way... (-(x//-y),x%-y) – Martin Ender – 2017-12-11T12:55:58.650

@MartinEnder Thats really good – Halvard Hummel – 2017-12-11T12:57:06.807

@Mr.Xcoder Included both – Halvard Hummel – 2017-12-11T12:59:29.857

8

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]

Mr. Xcoder

Posted 2017-12-11T12:17:13.587

Reputation: 39 774

6

Haskell, 25 bytes

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

Try it online!

totallyhuman

Posted 2017-12-11T12:17:13.587

Reputation: 15 378

5

Mathematica, 21 bytes

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

Try it online!

J42161217

Posted 2017-12-11T12:17:13.587

Reputation: 15 931

Can you add an explanation, please? – Rod – 2017-12-11T12:49:42.190

2@Rod ⌈#/#2⌉ calculates the ceiling of their division, and stores it in a variable s, and then subtracts argument 2 * s from argument 1. – Mr. Xcoder – 2017-12-11T12:51:03.427

1@Mr.Xcoder you are fast! – J42161217 – 2017-12-11T12:55:20.533

5

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.

Mr. Xcoder

Posted 2017-12-11T12:17:13.587

Reputation: 39 774

Ah yes, forgot that the divmod works with negative numbers :) – Emigna – 2017-12-11T13:09:29.327

And also, that's new functionality of J isn't it?. Never seen that before. Could definitely be useful. – Emigna – 2017-12-11T13:11:31.413

@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 – Mr. Xcoder – 2017-12-11T13:13:30.380

It's definitely new. Tried on my local version from August and 5)6 gives ['5']6 :) – Emigna – 2017-12-11T13:17:40.517

4

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.

Martin Ender

Posted 2017-12-11T12:17:13.587

Reputation: 184 808

4

Pari/GP, 18 bytes

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

Try it online!

alephalpha

Posted 2017-12-11T12:17:13.587

Reputation: 23 988

3

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

Giuseppe

Posted 2017-12-11T12:17:13.587

Reputation: 21 077

3

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.

Uriel

Posted 2017-12-11T12:17:13.587

Reputation: 11 708

2

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

Arnauld

Posted 2017-12-11T12:17:13.587

Reputation: 111 334

You can probably q=(a+b-1)/b+|0 instead q=a/b+.9|0 – Rod – 2017-12-11T13:01:34.340

@ETHproductions Sounds like a plan. ;) – Arnauld – 2017-12-12T07:30:40.603

2

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!

Galen Ivanov

Posted 2017-12-11T12:17:13.587

Reputation: 13 815

2

R, 31 29 bytes

-2 bytes thanks to Giuseppe

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

Try it online!

user2390246

Posted 2017-12-11T12:17:13.587

Reputation: 1 391

1I think you can do a 29 byter with -c(e%/%-o,-e%%o) – Giuseppe – 2017-12-11T14:35:08.760

2

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

Kaz

Posted 2017-12-11T12:17:13.587

Reputation: 372

1

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.

Uriel

Posted 2017-12-11T12:17:13.587

Reputation: 11 708

1

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

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

Try it online!

Xcali

Posted 2017-12-11T12:17:13.587

Reputation: 7 671

1

Commentator, 90 bytes

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

Try it online!

Outputs the remainder, then the quotient, newline separated.

caird coinheringaahing

Posted 2017-12-11T12:17:13.587

Reputation: 13 702

0

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!

Giacomo Garabello

Posted 2017-12-11T12:17:13.587

Reputation: 1 419

0

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

Bernat

Posted 2017-12-11T12:17:13.587

Reputation: 181

0

Add++, 35 bytes

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

Try it online!

caird coinheringaahing

Posted 2017-12-11T12:17:13.587

Reputation: 13 702

0

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

caird coinheringaahing

Posted 2017-12-11T12:17:13.587

Reputation: 13 702

0

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

PrincePolka

Posted 2017-12-11T12:17:13.587

Reputation: 653

0

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.

Giuseppe

Posted 2017-12-11T12:17:13.587

Reputation: 21 077

0

Swift, 47 bytes

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

Herman L

Posted 2017-12-11T12:17:13.587

Reputation: 3 611

0

TXR: 8 bytes

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

Kaz

Posted 2017-12-11T12:17:13.587

Reputation: 372

0

Clean, 42 bytes

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

Try it online!

Οurous

Posted 2017-12-11T12:17:13.587

Reputation: 7 916