Typescript Numeric Literal Types How To Find Nth Root of X

type CreateArrayWithLengthX<
LENGTH extends number,
ACC extends unknown[] = [],
> = ACC['length'] extends LENGTH
? ACC
: CreateArrayWithLengthX<LENGTH, [...ACC,1]>
type Multiplication<X extends number, Y extends number, Z extends number[] = [], V extends unknown[] = []> =
[...CreateArrayWithLengthX<Y>]['length'] extends Z['length']
? V
: Multiplication<X,Y,[1,...Z],[...CreateArrayWithLengthX<X>,...V]>
type Exponentiation<X extends number, N extends number, Counter extends number[] =[], Acc extends unknown[] = [1]> =
Counter['length'] extends N
? Acc // modified
: Exponentiation<X, N, [1, ...Counter], Multiplication<Acc['length'],X> >
type Root <X extends number, N extends number, Counter extends number[] = []> =
[...Exponentiation<Counter['length'], N>]['length'] extends X
? Counter['length']
: Root<X, N, [1,...Counter]>
type A = Root<4,2> // 2
type B = Root<8,3> // 2
type C = Root<81,4> // 3
type D = Root<625,4> // 5
type E = Root<1024,10> // 2
type F = Root<2187,7> // 3
type G = Root<9261,3> // 21

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store