Skip to content

Num.Prime

Num / Prime

Import

typescript
import { Num } from '@wollybeard/kit'

// Access via namespace
Num.Prime.someFunction()
typescript
import * as Num from '@wollybeard/kit/num'

// Access via namespace
Num.Prime.someFunction()

Functions

[F] is

typescript
(value: unknown): boolean

Parameters:

  • value - The value to check

Returns: True if value is a prime number

Type predicate to check if value is a prime number. Uses trial division optimization up to sqrt(n).

[F] from

typescript
(value: Natural): Prime

Parameters:

  • value - The number to convert to Prime

Returns: The value as a Prime number

Throws:

  • Error if value is not prime

Construct a Prime number. Throws if the value is not prime.

[F] tryFrom

typescript
(value: Natural): Prime | null

Parameters:

  • value - The number to try converting

Returns: The Prime number or null

Try to construct a Prime number. Returns null if the value is not prime.

[F] next

typescript
(value: number): Prime

Parameters:

  • value - Starting point (exclusive)

Returns: The next prime number

Find the next prime number after the given value.

[F] prev

typescript
(value: number): Prime | null

Parameters:

  • value - Starting point (exclusive)

Returns: The previous prime number or null

Find the previous prime number before the given value. Returns null if no prime exists before the value (i.e., value = 2).

[F] nth

typescript
(n: Natural): Prime

Parameters:

  • n - Which prime to get (1 = first prime = 2)

Returns: The nth prime number

Throws:

  • Error if n 1

Get the nth prime number (1-indexed). Uses a simple sieve for small n, trial division for larger.

[F] factorize

typescript
(value: Natural): Map<Prime, Natural>

Parameters:

  • value - The number to factorize (must be = 2)

Returns: Map of prime factors to exponents

Throws:

  • Error if value 2

Prime factorization of a number. Returns a map of prime factors to their exponents.

Types

[∩] Prime

typescript
type Prime = Natural & { [PrimeBrand]: true }

Prime number (natural number

1 with no divisors except 1 and itself).

Prime numbers are fundamental in mathematics and essential for:

  • Cryptography (RSA keys, Diffie-Hellman)
  • Hash table sizing (reduces collisions)
  • Random number generation
  • Number theory algorithms