MATL, 12 bytes
Qt:0hie=&fhq
Input is a, then b. Output remainder, quotient. Try it online!
This avoids modulo and division. It works as follows:
- Build an array of
a+1nonzero elements plus an appended0. - Reshapes as a 2D array of
brows. This automaticall pads with zeros if needed. - The row and column indices of the last nonzero entry, minus
1, are respectively the remainder and quotient.
Consider for example a=7, b=2.
Q % Take a implicitly. Push a+1
% STACK: 8
t: % Duplicate. Range from 1 to that
% STACK: 8, [1 2 3 4 5 6 7 8]
0h % Append a zero
% STACK: [1 2 3 4 5 6 7 8 0]
ie % Input b. Reshape as a matrix with b rows (in column major order)
% STACK: 7, [1 3 5 7 0;
2 4 6 8 0]
= % Compare for equality
% STACK: [0 0 0 0 0;
0 0 0 1 0]
&f % Row and column indices (1-based) of nonzero element
% STACK: 2, 4
hq % Concatenate. Subtract 1. Implicitly display
% STACK: [1 3]
Luis Mendo
- 106.7k
- 10
- 139
- 382