Skip to main content
added 9 characters in body
Source Link
noodle person
  • 10.3k
  • 1
  • 22
  • 76

TypeScript's Type System, 78 bytes

//@ts-ignore
type M<A,B,T=[]>=A extends[...B,...infer I]?M<I,B,[...T,1]>:[T,A]

Try it at the TypeScript Playground!

This submission is a generic type taking in two numbers in unary and outputting a tuple of two unary numbers, the first being the division and the second the remainder.

Explanation:

//@ts-ignore           // ignore any compiler errors in this type
type M<
  A,                   // A is the numerator
  B,                   // B is the denominator
  T = []               // T is the # of times recursed (division result)
> =
  A extends [...B,     // if B fits in A (A >= B):
    ...infer I         //   set I to the difference (A - B)
  ] ? M<               //   recurse with:
        I,             //     I as the numerator
        B,             //     B as the denominator
        [...T, 1]      //     T with 1 appended (i.e. increment)
      >
    : [T, A]           // otherwise return [T, A]

This uses tail recursion to increase the maximum recursion depth from 50 to 999.

TypeScript's Type System, 78 bytes

//@ts-ignore
type M<A,B,T=[]>=A extends[...B,...infer I]?M<I,B,[...T,1]>:[T,A]

Try it at the TypeScript Playground!

This submission is a generic type taking in two numbers in unary and outputting a tuple of two unary numbers, the first being the division and the second the remainder.

Explanation:

//@ts-ignore           // ignore any compiler errors in this type
type M<
  A,                   // A is the numerator
  B,                   // B is the denominator
  T = []               // T is the # of times recursed (division result)
> =
  A extends [...B,     // if B fits in A:
    ...infer I         //   set I to the difference (A - B)
  ] ? M<               //   recurse with:
        I,             //     I as the numerator
        B,             //     B as the denominator
        [...T, 1]      //     T with 1 appended (i.e. increment)
      >
    : [T, A]           // otherwise return [T, A]

This uses tail recursion to increase the maximum recursion depth from 50 to 999.

TypeScript's Type System, 78 bytes

//@ts-ignore
type M<A,B,T=[]>=A extends[...B,...infer I]?M<I,B,[...T,1]>:[T,A]

Try it at the TypeScript Playground!

This submission is a generic type taking in two numbers in unary and outputting a tuple of two unary numbers, the first being the division and the second the remainder.

Explanation:

//@ts-ignore           // ignore any compiler errors in this type
type M<
  A,                   // A is the numerator
  B,                   // B is the denominator
  T = []               // T is the # of times recursed (division result)
> =
  A extends [...B,     // if B fits in A (A >= B):
    ...infer I         //   set I to the difference (A - B)
  ] ? M<               //   recurse with:
        I,             //     I as the numerator
        B,             //     B as the denominator
        [...T, 1]      //     T with 1 appended (i.e. increment)
      >
    : [T, A]           // otherwise return [T, A]

This uses tail recursion to increase the maximum recursion depth from 50 to 999.

Source Link
noodle person
  • 10.3k
  • 1
  • 22
  • 76

TypeScript's Type System, 78 bytes

//@ts-ignore
type M<A,B,T=[]>=A extends[...B,...infer I]?M<I,B,[...T,1]>:[T,A]

Try it at the TypeScript Playground!

This submission is a generic type taking in two numbers in unary and outputting a tuple of two unary numbers, the first being the division and the second the remainder.

Explanation:

//@ts-ignore           // ignore any compiler errors in this type
type M<
  A,                   // A is the numerator
  B,                   // B is the denominator
  T = []               // T is the # of times recursed (division result)
> =
  A extends [...B,     // if B fits in A:
    ...infer I         //   set I to the difference (A - B)
  ] ? M<               //   recurse with:
        I,             //     I as the numerator
        B,             //     B as the denominator
        [...T, 1]      //     T with 1 appended (i.e. increment)
      >
    : [T, A]           // otherwise return [T, A]

This uses tail recursion to increase the maximum recursion depth from 50 to 999.