Hone logo
Hone
Problems

Type-Level Greatest Common Divisor (GCD) in TypeScript

This challenge involves implementing the Greatest Common Divisor (GCD) algorithm using TypeScript's advanced type-level programming capabilities. The goal is to create a type that, given two numeric literals, computes their GCD at compile time. This demonstrates the power of TypeScript for performing complex computations without runtime execution, which can be invaluable for static analysis and robust code generation.

Problem Description

Your task is to create a TypeScript type, let's call it GCD<A, B>, that takes two non-negative integer numeric literals A and B as type arguments. This type should resolve to a new numeric literal representing the greatest common divisor of A and B.

The GCD is the largest positive integer that divides both numbers without leaving a remainder. You should implement the Euclidean algorithm for GCD at the type level.

Key Requirements:

  • The GCD<A, B> type must correctly compute the GCD for any two non-negative integer numeric literals.
  • The implementation should be purely at the type level, using TypeScript's conditional types, inferring types, and arithmetic operations on numeric literals.
  • The solution should handle the edge cases where one or both numbers are zero.

Expected Behavior:

  • GCD<12, 18> should resolve to 6.
  • GCD<7, 5> should resolve to 1.
  • GCD<100, 0> should resolve to 100.
  • GCD<0, 42> should resolve to 42.
  • GCD<0, 0> should resolve to 0.

Edge Cases:

  • Consider cases where one of the input numbers is zero. The GCD of any number and zero is the absolute value of that number.
  • Consider the case where both input numbers are zero.

Examples

Example 1:

type Result = GCD<12, 18>;
// Expected: 6

Explanation: The greatest common divisor of 12 and 18 is 6.

Example 2:

type Result = GCD<48, 18>;
// Expected: 6

Explanation: The greatest common divisor of 48 and 18 is 6.

Example 3:

type Result = GCD<17, 23>;
// Expected: 1

Explanation: 17 and 23 are prime numbers and have no common factors other than 1.

Example 4:

type Result = GCD<100, 0>;
// Expected: 100

Explanation: The GCD of any number and 0 is that number itself.

Constraints

  • Input types A and B will be non-negative integer numeric literals.
  • The maximum value of A and B will be within the practical limits of TypeScript's numeric literal handling (typically up to Number.MAX_SAFE_INTEGER, though extreme values might hit performance limits).
  • The solution must be a type-level computation; no runtime JavaScript code should be involved in the GCD calculation itself.

Notes

The Euclidean algorithm is defined recursively:

  • gcd(a, 0) = a
  • gcd(a, b) = gcd(b, a mod b)

You will need to define helper types to perform subtraction and the modulo operation at the type level. Consider how to represent and manipulate numbers using TypeScript's existing type system features. Think about how to handle the recursive nature of the algorithm using conditional types. Good luck!

Loading editor...
typescript