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 Answer

Commonmark migration
Source Link

MATL, (削除) 12 (削除ここまで) 10 bytes

Qt:ie=&fhq

Input is a, then b. Output is remainder, then quotient.

Try it online!

###Explanation

Explanation

This avoids both modulo and division. Instead it uses array reshaping:

  1. Build an array of a+1 nonzero elements.
  2. Reshape as a 2D array of b rows. This automatically 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=3.

Q % Input a implicitly. Push a+1
 % STACK: 8
t: % Duplicate. Range from 1 to that
 % STACK: 8, [1 2 3 4 5 6 7 8]
ie % Input b. Reshape as a matrix with b rows (in column major order)
 % STACK: 8, [1 4 7;
 2 5 8]
 3 6 0]
= % Compare for equality
 % STACK: [0 0 0;
 0 0 1;
 0 0 0]
&f % Row and column indices (1-based) of nonzero element
 % STACK: 2, 3
hq % Concatenate. Subtract 1. Implicitly display
 % STACK: [1 2]

MATL, (削除) 12 (削除ここまで) 10 bytes

Qt:ie=&fhq

Input is a, then b. Output is remainder, then quotient.

Try it online!

###Explanation

This avoids both modulo and division. Instead it uses array reshaping:

  1. Build an array of a+1 nonzero elements.
  2. Reshape as a 2D array of b rows. This automatically 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=3.

Q % Input a implicitly. Push a+1
 % STACK: 8
t: % Duplicate. Range from 1 to that
 % STACK: 8, [1 2 3 4 5 6 7 8]
ie % Input b. Reshape as a matrix with b rows (in column major order)
 % STACK: 8, [1 4 7;
 2 5 8]
 3 6 0]
= % Compare for equality
 % STACK: [0 0 0;
 0 0 1;
 0 0 0]
&f % Row and column indices (1-based) of nonzero element
 % STACK: 2, 3
hq % Concatenate. Subtract 1. Implicitly display
 % STACK: [1 2]

MATL, (削除) 12 (削除ここまで) 10 bytes

Qt:ie=&fhq

Input is a, then b. Output is remainder, then quotient.

Try it online!

Explanation

This avoids both modulo and division. Instead it uses array reshaping:

  1. Build an array of a+1 nonzero elements.
  2. Reshape as a 2D array of b rows. This automatically 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=3.

Q % Input a implicitly. Push a+1
 % STACK: 8
t: % Duplicate. Range from 1 to that
 % STACK: 8, [1 2 3 4 5 6 7 8]
ie % Input b. Reshape as a matrix with b rows (in column major order)
 % STACK: 8, [1 4 7;
 2 5 8]
 3 6 0]
= % Compare for equality
 % STACK: [0 0 0;
 0 0 1;
 0 0 0]
&f % Row and column indices (1-based) of nonzero element
 % STACK: 2, 3
hq % Concatenate. Subtract 1. Implicitly display
 % STACK: [1 2]
added 6 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, (削除) 12 (削除ここまで) 10 bytes

Qt:ie=&fhq

Input is a, then b. Output is remainder, then quotient.

Try it online!

###Explanation

This avoids both modulo and division, and instead. Instead it uses array reshapingarray reshaping:

  1. Build an array of a+1 nonzero elements.
  2. Reshape as a 2D array of b rows. This automatically 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=3.

Q % Input a implicitly. Push a+1
 % STACK: 8
t: % Duplicate. Range from 1 to that
 % STACK: 8, [1 2 3 4 5 6 7 8]
ie % Input b. Reshape as a matrix with b rows (in column major order)
 % STACK: 8, [1 4 7;
 2 5 8]
 3 6 0]
= % Compare for equality
 % STACK: [0 0 0;
 0 0 1;
 0 0 0]
&f % Row and column indices (1-based) of nonzero element
 % STACK: 2, 3
hq % Concatenate. Subtract 1. Implicitly display
 % STACK: [1 2]

MATL, (削除) 12 (削除ここまで) 10 bytes

Qt:ie=&fhq

Input is a, then b. Output is remainder, then quotient.

Try it online!

###Explanation

This avoids modulo and division, and instead uses array reshaping:

  1. Build an array of a+1 nonzero elements.
  2. Reshape as a 2D array of b rows. This automatically 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=3.

Q % Input a implicitly. Push a+1
 % STACK: 8
t: % Duplicate. Range from 1 to that
 % STACK: 8, [1 2 3 4 5 6 7 8]
ie % Input b. Reshape as a matrix with b rows (in column major order)
 % STACK: 8, [1 4 7;
 2 5 8]
 3 6 0]
= % Compare for equality
 % STACK: [0 0 0;
 0 0 1;
 0 0 0]
&f % Row and column indices (1-based) of nonzero element
 % STACK: 2, 3
hq % Concatenate. Subtract 1. Implicitly display
 % STACK: [1 2]

MATL, (削除) 12 (削除ここまで) 10 bytes

Qt:ie=&fhq

Input is a, then b. Output is remainder, then quotient.

Try it online!

###Explanation

This avoids both modulo and division. Instead it uses array reshaping:

  1. Build an array of a+1 nonzero elements.
  2. Reshape as a 2D array of b rows. This automatically 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=3.

Q % Input a implicitly. Push a+1
 % STACK: 8
t: % Duplicate. Range from 1 to that
 % STACK: 8, [1 2 3 4 5 6 7 8]
ie % Input b. Reshape as a matrix with b rows (in column major order)
 % STACK: 8, [1 4 7;
 2 5 8]
 3 6 0]
= % Compare for equality
 % STACK: [0 0 0;
 0 0 1;
 0 0 0]
&f % Row and column indices (1-based) of nonzero element
 % STACK: 2, 3
hq % Concatenate. Subtract 1. Implicitly display
 % STACK: [1 2]
deleted 38 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 12(削除) 12 (削除ここまで) 10 bytes

Qt:0hie=&fhqie=&fhq

Input is a, then b. Output is remainder, then quotient.Try it online!

Try it online!

###Explanation

This avoids modulo and division. It works as follows, and instead uses array reshaping:

  1. Build an array of a+1 nonzero elements plus an appended 0.
  2. ReshapesReshape as a 2D array of b rows. This automaticallautomatically 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=2b=3.

Q % TakeInput 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: 78, [1 34 57;
 7 0; 2 5 8]
 2 43 6 8 0]
= % Compare for equality
 % STACK: [0 0 0;
  0 0 0;1;
 0 0 0 1 0]
&f % Row and column indices (1-based) of nonzero element
 % STACK: 2, 43
hq % Concatenate. Subtract 1. Implicitly display
 % STACK: [1 3]2]

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]

MATL, (削除) 12 (削除ここまで) 10 bytes

Qt:ie=&fhq

Input is a, then b. Output is remainder, then quotient.

Try it online!

###Explanation

This avoids modulo and division, and instead uses array reshaping:

  1. Build an array of a+1 nonzero elements.
  2. Reshape as a 2D array of b rows. This automatically 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=3.

Q % Input a implicitly. Push a+1
 % STACK: 8
t: % Duplicate. Range from 1 to that
 % STACK: 8, [1 2 3 4 5 6 7 8]
ie % Input b. Reshape as a matrix with b rows (in column major order)
 % STACK: 8, [1 4 7;
  2 5 8]
 3 6 0]
= % Compare for equality
 % STACK: [0 0 0;
  0 0 1;
 0 0 0]
&f % Row and column indices (1-based) of nonzero element
 % STACK: 2, 3
hq % Concatenate. Subtract 1. Implicitly display
 % STACK: [1 2]
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382
Loading

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