Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Revisions

1 of 4
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

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:

  1. Build an array of a+1 nonzero elements plus an appended 0.
  2. Reshapes as a 2D array of b rows. This automaticall pads with zeros if needed.
  3. 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

AltStyle によって変換されたページ (->オリジナル) /