Hacker News new | past | comments | ask | show | jobs | submit login

Here's an implementation of a prime finding algorithm in pure (only type-level) Typescript.

    type IsPrime<
      MaybePrime extends number,
      NumAsTuple extends unknown[] = [],
      RemainderAsTuple extends unknown[] = NumAsTuple,
    > =
      MaybePrime extends NumAsTuple["length"]
        ? NumAsTuple["length"] extends RemainderAsTuple["length"]
          ? NumAsTuple["length"] extends (1 | 2 | 3 | 4 | 5 | 6)
            ? NumAsTuple["length"] extends (1 | 2 | 3 | 5)
              ? true
              : false
            : NumAsTuple extends [infer _, infer _, infer _, infer _, infer _, infer _, ...infer Rest]
              ? IsPrime<MaybePrime, NumAsTuple, Rest>
              : false
          : RemainderAsTuple["length"] extends (1 | 2 | 3 | 4 | 5 | 6)
            ? RemainderAsTuple["length"] extends (1 | 5)
              ? true
              : false
            : RemainderAsTuple extends [infer _, infer _, infer _, infer _, infer _, infer _, ...infer Rest]
              ? IsPrime<MaybePrime, NumAsTuple, Rest>
              : false
        : IsPrime<MaybePrime, [...NumAsTuple, 1]>;



thank you! here[1] it is in action in the playground.

[1]: https://www.typescriptlang.org/play?#code/C4TwDgpgBAkgzgBQE4...


Cool. How large prime can it detect before it hits the recursion limit?


857




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: