Skip to content

PrimeNumbers ​

The prime numbers 2,3,5,7,11,…2, 3, 5, 7, 11, … as a lazy indexed collection, unranked by position.

PrimeNumbersthe primes in increasing order, an infinite indexed collection.

Domain: Collections

Details
  • A lazy indexed collection: Count(PrimeNumbers)=+∞Count(PrimeNumbers) = +\infty, and At(PrimeNumbers,k)At(PrimeNumbers, k) unranks the kk-th prime without ever sieving a full prefix -- At(PrimeNumbers,5)=11At(PrimeNumbers, 5) = 11.
  • OEIS A000040.
  • Membership goes through Element: Element(11,PrimeNumbers)Element(11, PrimeNumbers) is true, Element(9,PrimeNumbers)Element(9, PrimeNumbers) is false.
  • Distinct from Primes, compute-engine's own set<integer> constant: a value can't be extended, so the two don't share one name.
  • Past 2532^{53} membership is a Baillie–PSW test: a proof below 2642^{64}, and above it a probable-prime test (no composite is known to pass it), as Wolfram's PrimeQ is. Past 2163842^{16384}, and for the position of a prime past about 5⋅10115 \cdot 10^{11}, Element and the rank stay unevaluated.

Definition

Epsil, as the implementation runs it.

count()

PositiveInfinity

unrank(_r)

Apply(
       (sn_f: integer) => sn_f if sn_f > 0 else `NaN`,
       Fold(
             Function(sn_s if sn_s > 0 else (sn_k if sn_s + _r == 0 else sn_s -
                                                                         1) if sn_k >=
                                                                              2 &&
                                                                              Fold(
                                                                                    Function(ok_pd &&
                                                                                             sn_k % pd !=
                                                                                             0, ok_pd, pd),
                                                                                    True,
                                                                                    Range(2, Floor(Sqrt(sn_k)), 1),
                                                                                  ) else sn_s, sn_s, sn_k),
             0,
             Range(
                    1,
                    Apply(
                           (pb_n: integer) => 11 if pb_n < 6 else Floor(pb_n *
                                                                        (Ln(pb_n) +
                                                                         Ln(Ln(pb_n)))) + 1,
                           _r + 1,
                         ),
                    1,
                  ),
           ),
     )

rank(_x)

Fold(
      Function(sn_c + 1 if sn_k >= 2 && Fold(
                                              Function(
                                                        ok_pd && sn_k % pd != 0,
                                                        ok_pd,
                                                        pd,
                                                      ),
                                              True,
                                              Range(2, Floor(Sqrt(sn_k)), 1),
                                            ) else sn_c, sn_c, sn_k),
      0,
      Range(1, _x - 1, 1),
    ) if (_x >= 2 && Fold(
                          Function(ok_pd && _x % pd != 0, ok_pd, pd),
                          True,
                          Range(2, Floor(Sqrt(_x)), 1),
                        ) if _x >= 1 else False) else -1

valid(_x)

_x >= 2 && Fold(
                 Function(ok_pd && _x % pd != 0, ok_pd, pd),
                 True,
                 Range(2, Floor(Sqrt(_x)), 1),
               ) if _x >= 1 else False

Enumeration

Examples

2 more kept as data, checked against the oracles
Basic #

The first five primes

live ↗
InTake(PrimeNumbers,5)\mathrm{Take}(\mathrm{PrimeNumbers}, 5)Out[2,3,5,7,11]\bigl\lbrack2, 3, 5, 7, 11\bigr\rbrack

Unranks the fifth prime without sieving a prefix

live ↗
InPrimeNumbers[5]\mathrm{PrimeNumbers}[5]Out1111
About
Examples4
CrosswalkOEISSageMathmathlib4MATLAB

See also: Primes, Count, At, Element, SquareNumbers, AbundantNumbers, SmoothNumbers