ntheory(3pm)

โœจ NAME โœจ

ntheory โ€” Number theory utilities. It's the ntheory(3pm) page from the User Contributed Perl Documentation corner, and she's literally here to hand you every number theory tool in the group chat. ๐ŸŽ€๐Ÿ’–

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€

See Math::Prime::Util for complete documentation. That's the full lore, bestie. ๐Ÿ“šโœจ

๐ŸŒˆ QUICK REFERENCE ๐ŸŒˆ

Okay so this is the quick-reference cheat sheet. Keep up, no cap. ๐Ÿ’…

๐Ÿท๏ธ Tags ๐Ÿท๏ธ

  • :all โ€” to import all functions (other than NON-EXPORTED below). ๐Ÿซถ
  • :rand โ€” to import rand, srand, irand, irand64. ๐Ÿ”ฅ

๐Ÿ”ฅ PRIMALITY ๐Ÿ”ฅ

  • is_prob_prime(n) โ€” primality test (BPSW). ๐Ÿ’–
  • is_prime(n) โ€” primality test (BPSW + extra). โœจ
  • is_provable_prime(n) โ€” primality test with proof. ๐Ÿ’…
  • is_provable_prime_with_cert(n) โ€” primality test: (isprime,cert). ๐Ÿ“œ
  • prime_certificate(n) โ€” as above with just certificate. ๐ŸŽ€
  • verify_prime(cert) โ€” verify a primality certificate. โœ…
  • is_mersenne_prime(p) โ€” is 2^p-1 prime or composite. ๐Ÿงœ
  • is_aks_prime(n) โ€” AKS deterministic test (slow). ๐ŸŒ she said what she said
  • is_ramanujan_prime(n) โ€” is n a Ramanujan prime. ๐Ÿ‘‘
  • is_gaussian_prime(a,b) โ€” is a+bi a Gaussian prime. ๐ŸŒธ

๐Ÿ’ซ PROBABLE PRIME TESTS ๐Ÿ’ซ

  • is_pseudoprime(n,bases) โ€” Fermat probable prime test. โœจ
  • is_euler_pseudoprime(n,bases) โ€” Euler test to bases. โœจ
  • is_euler_plumb_pseudoprime(n) โ€” Euler Criterion test. โœจ
  • is_strong_pseudoprime(n,bases) โ€” Miller-Rabin test to bases. โœจ
  • is_lucas_pseudoprime(n) โ€” Lucas test. โœจ
  • is_strong_lucas_pseudoprime(n) โ€” strong Lucas test. ๐Ÿ’ช
  • is_almost_extra_strong_lucas_pseudoprime(n, [incr]) โ€” AES Lucas test. ๐Ÿ’…
  • is_extra_strong_lucas_pseudoprime(n) โ€” extra strong Lucas test. ๐Ÿ’ช
  • is_frobenius_pseudoprime(n, [a,b]) โ€” Frobenius quadratic test. ๐Ÿง™
  • is_frobenius_underwood_pseudoprime(n) โ€” combined PSP and Lucas. ๐Ÿ”€
  • is_frobenius_khashin_pseudoprime(n) โ€” Khashin's 2013 Frobenius test. ๐Ÿ“…
  • is_perrin_pseudoprime(n [,r]) โ€” Perrin test. โœจ
  • is_catalan_pseudoprime(n) โ€” Catalan test. โœจ
  • is_bpsw_prime(n) โ€” combined SPSP-2 and ES Lucas. ๐Ÿ”€
  • miller_rabin_random(n, ntests) โ€” perform random-base MR tests. ๐ŸŽฒ

๐Ÿ‘‘ PRIMES ๐Ÿ‘‘

  • primes([start,] end) โ€” array ref of primes. ๐Ÿ’–
  • prime_powers([start,] end) โ€” array ref of prime powers. โœจ
  • twin_primes([start,] end) โ€” array ref of twin primes. ๐Ÿ‘ฏ
  • semi_primes([start,] end) โ€” array ref of semiprimes. ๐ŸŒ—
  • almost_primes(k, [start,] end) โ€” array ref of k-almost-primes. ๐Ÿงฎ
  • omega_primes(k, [start,] end) โ€” array ref of k-omega-primes. ฮฉ
  • ramanujan_primes([start,] end) โ€” array ref of Ramanujan primes. ๐Ÿ‘‘
  • sieve_prime_cluster(start, end, @C) โ€” list of prime k-tuples. ๐Ÿ•ธ๏ธ
  • sieve_range(n, width, depth) โ€” sieve out small factors to depth. ๐Ÿงน
  • next_prime(n) โ€” next prime > n. โžก๏ธ
  • prev_prime(n) โ€” previous prime < n. โฌ…๏ธ
  • next_prime_power(n) โ€” next prime power > n. โžก๏ธ
  • prev_prime_power(n) โ€” previous prime power < n. โฌ…๏ธ
  • prime_count(n) โ€” count of primes <= n. ๐Ÿ”ข
  • prime_count(start, end) โ€” count of primes in range. ๐Ÿ“
  • prime_count_lower(n) โ€” fast lower bound for prime count. โฌ‡๏ธ
  • prime_count_upper(n) โ€” fast upper bound for prime count. โฌ†๏ธ
  • prime_count_approx(n) โ€” fast approximate prime count. ใ€ฐ๏ธ
  • prime_power_count(n) โ€” count of prime powers <= n. ๐Ÿ”ข
  • prime_power_count(start, end) โ€” count of prime powers in range. ๐Ÿ“
  • prime_power_count_lower(n) โ€” fast lower bound for prime power count. โฌ‡๏ธ
  • prime_power_count_upper(n) โ€” fast upper bound for prime power count. โฌ†๏ธ
  • prime_power_count_approx(n) โ€” fast approximate prime power count. ใ€ฐ๏ธ
  • nth_prime(n) โ€” the nth prime (n=1 returns 2). ๐Ÿฅ‡
  • nth_prime_lower(n) โ€” fast lower bound for nth prime. โฌ‡๏ธ
  • nth_prime_upper(n) โ€” fast upper bound for nth prime. โฌ†๏ธ
  • nth_prime_approx(n) โ€” fast approximate nth prime. ใ€ฐ๏ธ
  • nth_prime_power(n) โ€” the nth prime power (n=1 returns 2). ๐Ÿฅ‡
  • nth_prime_power_lower(n) โ€” fast lower bound for nth prime power. โฌ‡๏ธ
  • nth_prime_power_upper(n) โ€” fast upper bound for nth prime power. โฌ†๏ธ
  • nth_prime_power_approx(n) โ€” fast approximate nth prime power. ใ€ฐ๏ธ
  • twin_prime_count(n) โ€” count of twin primes <= n. ๐Ÿ‘ฏ
  • twin_prime_count(start, end) โ€” count of twin primes in range. ๐Ÿ“
  • twin_prime_count_approx(n) โ€” fast approximate twin prime count. ใ€ฐ๏ธ
  • nth_twin_prime(n) โ€” the nth twin prime (n=1 returns 3). ๐Ÿฅ‡
  • nth_twin_prime_approx(n) โ€” fast approximate nth twin prime. ใ€ฐ๏ธ
  • semiprime_count(n) โ€” count of semiprimes <= n. ๐ŸŒ—
  • semiprime_count(start, end) โ€” count of semiprimes in range. ๐Ÿ“
  • semiprime_count_approx(n) โ€” fast approximate semiprime count. ใ€ฐ๏ธ
  • nth_semiprime(n) โ€” the nth semiprime. ๐Ÿฅ‡
  • nth_semiprime_approx(n) โ€” fast approximate nth semiprime. ใ€ฐ๏ธ
  • almost_prime_count(k,n) โ€” count of k-almost-primes. ๐Ÿงฎ
  • almost_prime_count_approx(k,n) โ€” fast approximate k-almost-prime count. ใ€ฐ๏ธ
  • almost_prime_count_lower(k,n) โ€” fast k-almost-prime count lower bound. โฌ‡๏ธ
  • almost_prime_count_upper(k,n) โ€” fast k-almost-prime count upper bound. โฌ†๏ธ
  • nth_almost_prime(k,n) โ€” the nth number with exactly k factors. ๐Ÿฅ‡
  • nth_almost_prime_approx(k,n) โ€” fast approximate nth k-almost prime. ใ€ฐ๏ธ
  • nth_almost_prime_lower(k,n) โ€” fast nth k-almost prime lower bound. โฌ‡๏ธ
  • nth_almost_prime_upper(k,n) โ€” fast nth k-almost prime upper bound. โฌ†๏ธ
  • omega_prime_count(k,n) โ€” count divisible by exactly k primes. ฮฉ
  • nth_omega_prime(k,n) โ€” the nth number div by exactly k primes. ๐Ÿฅ‡
  • ramanujan_prime_count(n) โ€” count of Ramanujan primes <= n. ๐Ÿ‘‘
  • ramanujan_prime_count(start, end) โ€” count of Ramanujan primes in range. ๐Ÿ“
  • ramanujan_prime_count_lower(n) โ€” fast lower bound for Ramanujan count. โฌ‡๏ธ
  • ramanujan_prime_count_upper(n) โ€” fast upper bound for Ramanujan count. โฌ†๏ธ
  • ramanujan_prime_count_approx(n) โ€” fast approximate Ramanujan count. ใ€ฐ๏ธ
  • nth_ramanujan_prime(n) โ€” the nth Ramanujan prime (Rn). ๐Ÿฅ‡
  • nth_ramanujan_prime_lower(n) โ€” fast lower bound for Rn. โฌ‡๏ธ
  • nth_ramanujan_prime_upper(n) โ€” fast upper bound for Rn. โฌ†๏ธ
  • nth_ramanujan_prime_approx(n) โ€” fast approximate Rn. ใ€ฐ๏ธ
  • legendre_phi(n,a) โ€” # below n not div by first a primes. ๐Ÿง™
  • inverse_li(n) โ€” integer inverse logarithmic integral. ๐Ÿ”„
  • inverse_li_nv(x) โ€” float inverse logarithmic integral. ๐Ÿ”„
  • prime_precalc(n) โ€” precalculate primes to n. ๐Ÿง 
  • sum_primes([start,] end) โ€” return summation of primes in range. โž•
  • print_primes(start,end[,fd]) โ€” print primes to stdout or fd. ๐Ÿ–จ๏ธ

๐Ÿงฉ FACTORING ๐Ÿงฉ

  • factor(n) โ€” array of prime factors of n. ๐Ÿงฉ
  • factor_exp(n) โ€” array of [p,k] factors p^k. ๐Ÿงฎ
  • divisors(n) โ€” array of divisors of n. ๐Ÿฐ
  • divisor_sum(n) โ€” sum of divisors. โž•
  • divisor_sum(n,k) โ€” sum of k-th power of divisors. โž•
  • divisor_sum(n,sub{...}) โ€” sum of code run for each divisor. ๐Ÿ’ป

๐Ÿ” ITERATORS ๐Ÿ”

  • forprimes { ... } [start,] end โ€” loop over primes in range. ๐Ÿ”
  • forcomposites { ... } [start,] end โ€” loop over composites in range. ๐Ÿ”
  • foroddcomposites {...} [start,] end โ€” loop over odd composites in range. ๐Ÿ”
  • forsemiprimes {...} [start,] end โ€” loop over semiprimes in range. ๐Ÿ”
  • foralmostprimes {...} k,[beg,],end โ€” loop over k-almost-primes in range. ๐Ÿ”
  • forfactored {...} [start,] end โ€” loop with factors. ๐Ÿ”
  • forsquarefree {...} [start,] end โ€” loop with factors of square-free n. ๐Ÿ”
  • forsquarefreeint {...} [start,] end โ€” loop over square-free n. ๐Ÿ”
  • fordivisors { ... } n โ€” loop over the divisors of n. ๐Ÿ”
  • forpart { ... } n [,{...}] โ€” loop over integer partitions. ๐Ÿ”
  • forcomp { ... } n [,{...}] โ€” loop over integer compositions. ๐Ÿ”
  • forcomb { ... } n, k โ€” loop over combinations. ๐Ÿ”
  • forperm { ... } n โ€” loop over permutations. ๐Ÿ”
  • formultiperm { ... } \@n โ€” loop over multiset permutations. ๐Ÿ”
  • forderange { ... } n โ€” loop over derangements. ๐Ÿ”
  • forsetproduct { ... } \@a[,...] โ€” loop over Cartesian product of lists. ๐Ÿ”
  • prime_iterator([start]) โ€” returns a simple prime iterator. ๐Ÿ”
  • prime_iterator_object([start]) โ€” returns a prime iterator object. ๐Ÿ”
  • lastfor โ€” stop iteration of for.... loop. ๐Ÿ›‘

๐ŸŽฒ RANDOM NUMBERS ๐ŸŽฒ

  • irand() โ€” random 32-bit integer. ๐ŸŽฒ
  • irand64() โ€” random UV-bit integer (64 or 32). ๐ŸŽฒ
  • drand([limit]) โ€” random NV in [0,1) or [0,limit). ๐ŸŽฒ
  • random_bytes(n) โ€” string with n random bytes. ๐Ÿ”ข
  • entropy_bytes(n) โ€” string with n entropy-source bytes. ๐ŸŒช๏ธ
  • urandomb(n) โ€” random integer less than 2^n. ๐ŸŽฒ
  • urandomm(n) โ€” random integer less than n. ๐ŸŽฒ
  • csrand(data) โ€” seed the CSPRNG with binary data. ๐ŸŒฑ
  • srand([seed]) โ€” simple seed (exported with :rand). ๐ŸŒฑ
  • rand([limit]) โ€” alias for drand (exported with :rand). ๐ŸŽฒ
  • random_factored_integer(n) โ€” random [1..n] and array ref of factors. ๐ŸŽฒ

๐ŸŒŸ RANDOM PRIMES ๐ŸŒŸ

  • random_prime([start,] end) โ€” random prime in a range. ๐ŸŽฒ
  • random_ndigit_prime(n) โ€” random prime with n digits. ๐Ÿ”ข
  • random_nbit_prime(n) โ€” random prime with n bits. ๐Ÿ”ข
  • random_safe_prime(n) โ€” random safe prime with n bits. ๐Ÿ›ก๏ธ
  • random_strong_prime(n) โ€” random strong prime with n bits. ๐Ÿ’ช
  • random_proven_prime(n) โ€” random n-bit prime with proof. ๐Ÿ“œ
  • random_proven_prime_with_cert(n) โ€” as above and include certificate. ๐Ÿ“œ
  • random_maurer_prime(n) โ€” random n-bit prime w/ Maurer's alg. ๐Ÿง™
  • random_maurer_prime_with_cert(n) โ€” as above and include certificate. ๐Ÿ“œ
  • random_shawe_taylor_prime(n) โ€” random n-bit prime with S-T alg. ๐Ÿง™
  • random_shawe_taylor_prime_with_cert(n) โ€” as above including certificate. ๐Ÿ“œ
  • random_unrestricted_semiprime(n) โ€” random n-bit semiprime. ๐ŸŒ—
  • random_semiprime(n) โ€” as above with equal size factors. โš–๏ธ

๐ŸŒธ LISTS ๐ŸŒธ

okay so bestie, this section is a whole vibe โ€” we've got vector/list helpers and integer set helpers, all lined up and ready. every signature is here, no cap. ๐Ÿ‘‘

  • vecsum(@list) โ€” gives you the integer sum of the list. โœจ
  • vecprod(@list) โ€” same energy, but it's the integer product of the list. ๐Ÿ’…
  • vecmin(@list) โ€” finds the minimum of a list of integers. lowkey essential.
  • vecmax(@list) โ€” finds the maximum of a list of integers. highkey iconic.
  • vecuniq(@list) โ€” removes duplicates from a list of integers. so clean โœจ
  • vecsingleton(@list) โ€” removes all items that aren't unique, so only the one-of-a-kind ones stay. ๐Ÿ’€
  • vecfreq(@list) โ€” returns a hash where each item maps to its count in the list. iykyk.
  • vecsort(@list) โ€” numerically sorts a list of integers. periodt.
  • vecsorti(\@list) โ€” sorts a list ref in place, numerically. no copy, just vibes. ๐Ÿ’…
  • vecextract(\@list, mask) โ€” selects from the list based on a mask. it's giving filtered. ๐ŸŽ€
  • vecequal(\@list1, \@list2) โ€” compares two array refs for equality. are they twins? ๐Ÿ‘ฏ
  • vecreduce { ... } @list โ€” does a reduce / left fold over the list with your block. โœจ
  • vecall { ... } @list โ€” returns true if all elements make your block true. all or nothing.
  • vecany { ... } @list โ€” returns true if any element makes your block true. just one is enough.
  • vecnone { ... } @list โ€” returns true if none of the elements make your block true. zero vibes.
  • vecnotall { ... } @list โ€” returns true if not all elements make your block true. almost, bestie.
  • vecfirst { ... } @list โ€” returns the first value that makes your block evaluate true. ๐Ÿฅ‡
  • vecfirstidx { ... } @list โ€” returns the first index that makes your block evaluate true. where it at? ๐Ÿ”
  • vecmex(@list) โ€” returns the least non-negative value not in the list. mex, but make it fashion. ๐Ÿ’…
  • vecpmex(@list) โ€” returns the least positive value not in the list. pmex, periodt.
  • vecsample(k,@list) โ€” returns k random elements from the list. surprise, bestie! ๐ŸŽฒ
  • vecslide { ... } @list โ€” calls your block for each pair in the list. sliding through like ๐Ÿ›

  • toset(...) โ€” converts its arguments into an integer set (a unique sorted array ref). so organized. ๐Ÿงบ
  • setinsert(\@A,$v) โ€” inserts integer v into integer set A. welcome to the set. ๐Ÿซถ
  • setinsert(\@A,\@B) โ€” inserts all values from list B into integer set A. bulk add, no cap.
  • setremove(\@A,$v) โ€” removes integer v from integer set A. bye bestie. ๐Ÿ‘‹
  • setremove(\@A,\@B) โ€” removes all values from list B from integer set A. mass removal. ๐Ÿ’€
  • setinvert(\@A,$v) โ€” if v is in set A, removes it; otherwise adds it. flip flop. ๐Ÿ”„
  • setinvert(\@A,\@B) โ€” does that invert thing for every value in integer set B. invert the whole vibe. โœจ
  • setcontains(\@A,...) โ€” checks whether all the listed values are in integer set A. are they all here? ๐Ÿ‘€
  • setcontains(\@A,\@B) โ€” checks whether integer set B is a subset of integer set A. tiny but mighty.
  • setcontainsany(\@A,...) โ€” checks whether any of the listed values are in integer set A. just one? valid.
  • setcontainsany(\@A,\@B) โ€” checks whether any value in B is in integer set A. overlap check. ๐Ÿ”
  • setbinop { ... } \@A[,\@B] โ€” applies your operation block to all a,b pairs where a is from A and b is from B (B is optional). binary op energy. โšก
  • sumset(\@A[,\@B]) โ€” applies a+b to all a,b pairs where a is from A and b is from B (B is optional). math but cute. โž•
  • setunion(\@A,\@B) โ€” union of two integer lists. together forever. ๐Ÿซถ
  • setintersect(\@A,\@B) โ€” intersection of two integer lists. the overlap. ๐Ÿ’ž
  • setminus(\@A,\@B) โ€” difference of two integer lists. what's left over. โœจ
  • setdelta(\@A,\@B) โ€” symmetric difference of two integer lists. the exclusive or vibe. ๐Ÿ”€
  • is_sidon_set(\@L) โ€” checks if integer list L is a Sidon set. fancy. ๐Ÿ‘‘
  • is_sumfree_set(\@L) โ€” checks if integer list L is a sum-free set. no sums allowed. ๐Ÿšซ
  • set_is_disjoint(\@A,\@B) โ€” checks if set B is disjoint from set A. zero overlap. ๐Ÿšท
  • set_is_equal(\@A,\@B) โ€” checks if set B is equal to set A. same exact energy. ๐Ÿ’ฏ
  • set_is_subset(\@A,\@B) โ€” checks if set B is a subset of set A. smaller but included. ๐ŸŽ€
  • set_is_proper_subset(\@A,\@B) โ€” checks if set B is a proper subset of set A. subset but not equal. strict vibes.
  • set_is_superset(\@A,\@B) โ€” checks if set B is a superset of set A. bigger and contains all. ๐Ÿ‘‘
  • set_is_proper_superset(\@A,\@B) โ€” checks if set B is a proper superset of set A. superset but not equal. extra. ๐Ÿ’…
  • set_is_proper_intersection(\@A,\@B) โ€” checks if set B is a proper intersection of set A. intersection with rules. iykyk.

๐Ÿงฎ MATH ๐Ÿงฎ

Okay bestie, buckle up ๐Ÿ’– this is the whole number-theory toolbox, every single function, no skips, no cap. Let's go down the list together ๐ŸŒธ

  • todigits(n[,base[,len]]) โ€” turns n into an array of its digits in whatever base you want
  • todigitstring(n[,base[,len]]) โ€” same energy but you get a string back instead of an array ๐Ÿ’…
  • fromdigits(\@d,[,base]) โ€” takes an arrayref of digits and converts that base-digit vector back into a number
  • fromdigits(str,[,base]) โ€” takes a digit string and converts it back into a number โœจ
  • sumdigits(n) โ€” adds up all the digits of n, and yes there's an optional base
  • tozeckendorf(n) โ€” converts n into Zeckendorf / Fibbinary form ๐Ÿฆ‹
  • fromzeckendorf(str) โ€” the glow-up in reverse: a Zeckendorf binary string becomes a number again
  • is_odd(n) โ€” returns 1 if n is odd, 0 otherwise
  • is_even(n) โ€” returns 1 if n is even, 0 otherwise
  • is_divisible(n,d) โ€” returns 1 if n is divisible by d
  • is_congruent(n,c,d) โ€” returns 1 if n is congruent to c mod d
  • is_qr(a,n) โ€” returns 1 if a is a quadratic residue mod n
  • is_square(n) โ€” returns 1 if n is a perfect square ๐ŸŽ€
  • is_power(n) โ€” returns k if n = c^k for some integer c
  • is_power(n,k) โ€” returns 1 if n = c^k for an integer c and that specific k
  • is_power(n,k,\$root) โ€” same as above but it also drops c into $root for you
  • is_perfect_power(n) โ€” returns 1 if n = c^k where c != 0 and k > 1
  • is_prime_power(n) โ€” returns k if n = p^k for a prime p and k > 0
  • is_prime_power(n,\$p) โ€” same deal but it also sets $p to p ๐Ÿ’–
  • is_square_free(n) โ€” true when no prime factor shows up more than once (no repeated factors)
  • is_powerfree(n[,k]) โ€” is n free of any k-th powers?
  • is_cyclic(n) โ€” does n have only one group of order n?
  • is_carmichael(n) โ€” is n a Carmichael number?
  • is_quasi_carmichael(n) โ€” is n a quasi-Carmichael number?
  • is_primitive_root(r,n) โ€” is r a primitive root mod n?
  • is_pillai(n) โ€” gives you v where v! % n == n-1 and n % v != 1
  • is_semiprime(n) โ€” does n have exactly 2 prime factors? ๐Ÿ‘€
  • is_almost_prime(k,n) โ€” does n have exactly k prime factors?
  • is_omega_prime(k,n) โ€” is n divisible by exactly k primes?
  • is_chen_prime(n) โ€” is n prime and n+2 prime or semiprime?
  • is_polygonal(n,k) โ€” is n a k-polygonal number?
  • is_polygonal(n,k,\$root) โ€” same, but it also sets $root for you โœจ
  • is_sum_of_squares(n[,k]) โ€” is n a sum of k squares (default is 2)?
  • is_congruent_number(n) โ€” is n a congruent number?
  • is_perfect_number(n) โ€” is n equal to the sum of its proper divisors? iconic behavior tbh
  • is_fundamental(d) โ€” is d a fundamental discriminant?
  • is_totient(n) โ€” is n = euler_phi(x) for some x?
  • is_lucky(n) โ€” is n a lucky number? ๐Ÿ€
  • is_happy(n) โ€” if n is a happy number it returns the height ๐Ÿ˜Š
  • is_happy(n,base,exponent) โ€” same but for S_b_e happy numbers, returns the height
  • is_smooth(n,k) โ€” is n a k-smooth number?
  • is_rough(n,k) โ€” is n a k-rough number?
  • is_powerful(n[,k]) โ€” is n a k-powerful number?
  • is_practical(n) โ€” is n a practical number?
  • is_delicate_prime(n) โ€” is n a digitally delicate prime? ๐Ÿ™ˆ
  • powint(a,b) โ€” signed integer a^b
  • mulint(a,b) โ€” signed integer a * b
  • addint(a,b) โ€” signed integer a + b
  • subint(a,b) โ€” signed integer a - b
  • add1int(n) โ€” signed integer n + 1
  • sub1int(n) โ€” signed integer n - 1
  • divint(a,b) โ€” signed integer a / b (floor)
  • modint(a,b) โ€” signed integer a % b (floor)
  • cdivint(a,b) โ€” signed integer a / b (ceilint)
  • divrem(a,b) โ€” returns (quot,rem) of a/b (Euclidian)
  • fdivrem(a,b) โ€” returns (quot,rem) of a/b (floored)
  • cdivrem(a,b) โ€” returns (quot,rem) of a/b (ceiling)
  • tdivrem(a,b) โ€” returns (quot,rem) of a/b (truncated)
  • lshiftint(n,k) โ€” left shift n by k bits
  • rshiftint(n,k) โ€” right shift n by k bits (truncate)
  • rashiftint(n,k) โ€” right shift n by k bits (floor)
  • absint(n) โ€” integer absolute value
  • negint(n) โ€” integer negation
  • cmpint(a,b) โ€” integer comparison, works like <=>
  • signint(n) โ€” integer sign as -1, 0, or 1
  • sqrtint(n) โ€” integer square root
  • rootint(n,k) โ€” integer k-th root
  • rootint(n,k,\$rk) โ€” same but it also sets $rk to r^k ๐Ÿซถ
  • logint(n,b) โ€” integer logarithm
  • logint(n,b,\$be) โ€” same but it also sets $be to b^e
  • gcd(@list) โ€” greatest common divisor
  • lcm(@list) โ€” least common multiple
  • gcdext(x,y) โ€” returns (u,v,d) where u*x+v*y=d
  • chinese([a,mod1],[b,mod2],...) โ€” CRT that returns the remainder
  • chinese2([a,mod1],[b,mod2],...) โ€” CRT that returns (remainder,LCM)
  • frobenius_number(@list) โ€” the Frobenius Number of a set
  • primorial(n) โ€” product of all primes below n
  • pn_primorial(n) โ€” product of the first n primes
  • factorial(n) โ€” product of the first n integers, aka n!
  • factorialmod(n,m) โ€” factorial mod m
  • subfactorial(n) โ€” count of derangements of n objects
  • binomial(n,k) โ€” binomial coefficient
  • binomialmod(n,k,m) โ€” binomial(n,k) mod m
  • falling_factorial(x,n) โ€” falling factorial
  • rising_factorial(x,n) โ€” rising factorial
  • partitions(n) โ€” number of integer partitions
  • valuation(n,k) โ€” how many times n is divisible by k
  • hammingweight(n) โ€” population count, aka the number of binary 1s
  • kronecker(a,b) โ€” Kronecker (Jacobi) symbol
  • negmod(a,n) โ€” -a mod n
  • addmod(a,b,n) โ€” a + b mod n
  • submod(a,b,n) โ€” a - b mod n
  • mulmod(a,b,n) โ€” a * b mod n
  • muladdmod(a,b,c,n) โ€” a * b + c mod n
  • mulsubmod(a,b,c,n) โ€” a * b - c mod n
  • divmod(a,b,n) โ€” a / b mod n
  • powmod(a,b,n) โ€” a ^ b mod n ๐Ÿ”ฅ
  • invmod(a,n) โ€” inverse of a modulo n
  • sqrtmod(a,n) โ€” modular square root
  • rootmod(a,k,n) โ€” modular k-th root
  • allsqrtmod(a,n) โ€” list of all the modular square roots
  • allrootmod(a,k,n) โ€” list of all the modular k-th roots
  • cornacchia(d,n) โ€” finds x,y for x^2 + d * y^2 = n
  • prime_bigomega(n) โ€” number of prime factors
  • prime_omega(n) โ€” number of distinct prime factors
  • moebius(n) โ€” Moebius function of n
  • moebius(beg, end) โ€” list of Moebius values over a range
  • mertens(n) โ€” sum of Moebius from 1 to n
  • euler_phi(n) โ€” Euler totient of n
  • euler_phi(beg, end) โ€” Euler totient over a range
  • inverse_totient(n) โ€” the image of Euler totient
  • jordan_totient(k,n) โ€” Jordan's totient
  • sumtotient(n) โ€” sum of Euler totient from 1 to n
  • carmichael_lambda(n) โ€” Carmichael's Lambda function
  • ramanujan_sum(k,n) โ€” Ramanujan's sum
  • exp_mangoldt(n) โ€” exponential of the Mangoldt function
  • liouville(n) โ€” Liouville function
  • sumliouville(n) โ€” sum of Liouville from 1 to n
  • znorder(a,n) โ€” multiplicative order of a mod n
  • znprimroot(n) โ€” smallest primitive root
  • znlog(a, g, p) โ€” solves for k in a = g^k mod p
  • qnr(n) โ€” least quadratic non-residue
  • chebyshev_theta(n) โ€” first Chebyshev function
  • chebyshev_psi(n) โ€” second Chebyshev function
  • hclassno(n) โ€” Hurwitz class number H(n) * 12
  • ramanujan_tau(n) โ€” Ramanujan's Tau function
  • consecutive_integer_lcm(n) โ€” lcm(1 .. n)
  • lucasu(P, Q, k) โ€” U_k for Lucas(P,Q)
  • lucasv(P, Q, k) โ€” V_k for Lucas(P,Q)
  • lucasuv(P, Q, k) โ€” (U_k,V_k) for Lucas(P,Q)
  • lucasumod(P, Q, k, n) โ€” U_k for Lucas(P,Q) mod n
  • lucasvmod(P, Q, k, n) โ€” V_k for Lucas(P,Q) mod n
  • lucasuvmod(P, Q, k, n) โ€” (U_k,V_k,Q^k) for Lucas(P,Q) mod n
  • lucas_sequence(n,P,Q,k) โ€” deprecated, please use lucasuvmod instead bestie, fr fr
  • pisano_period(n) โ€” the period of Fibonacci numbers mod n
  • bernfrac(n) โ€” Bernoulli number as (num,den)
  • bernreal(n) โ€” Bernoulli number as a BigFloat
  • harmfrac(n) โ€” Harmonic number as (num,den)
  • harmreal(n) โ€” Harmonic number as a BigFloat
  • stirling(n,m,[type]) โ€” Stirling numbers, 1st or 2nd type, your pick โœจ
  • fubini(n) โ€” Fubini (Ordered Bell) number
  • numtoperm(n,k) โ€” the k-th lexico permutation of n elements
  • permtonum([a,b,...]) โ€” permutation number of the permutation you feed it
  • randperm(n,[k]) โ€” random permutation of n elements ๐ŸŽฒ
  • shuffle(...) โ€” random permutation of an array
  • lucky_numbers(n) โ€” array ref of the lucky sieve up to n
  • lucky_count(n) โ€” count of lucky numbers <= n
  • lucky_count(start, end) โ€” count of lucky numbers in a range
  • lucky_count_lower(n) โ€” fast lower bound for the lucky count
  • lucky_count_upper(n) โ€” fast upper bound for the lucky count
  • lucky_count_approx(n) โ€” fast approximate lucky count
  • nth_lucky(n) โ€” the n-th entry in the lucky sieve
  • nth_lucky_lower(n) โ€” fast lower bound for the n-th lucky number
  • nth_lucky_upper(n) โ€” fast upper bound for the n-th lucky number
  • nth_lucky_approx(n) โ€” fast approximate n-th lucky number
  • minimal_goldbach_pair(n) โ€” the least prime p where n-p is also prime
  • goldbach_pair_count(n) โ€” how many prime pairs sum to n
  • goldbach_pairs(n) โ€” array of every p where p and n-p are both prime
  • powerful_numbers([lo,]hi[,k]) โ€” array ref of k-powerful numbers from lo to hi
  • powerful_count(n[,k]) โ€” count of k-powerful numbers <= n
  • sumpowerful(n[,k]) โ€” sum of k-powerful numbers <= n
  • nth_powerful(n[,k]) โ€” the n-th k-powerful number
  • next_perfect_power(n) โ€” the next perfect power greater than n ๐Ÿ‘‘

  • prev_perfect_power(n) โ€” the perfect power that comes right before < n ๐Ÿ“
  • perfect_power_count(n) โ€” how many perfect powers are <= n
  • perfect_power_count(start, end) โ€” how many live inside that range
  • perfect_power_count_lower(n) โ€” fast lower bound for the perfect power count
  • perfect_power_count_upper(n) โ€” fast upper bound for the perfect power count
  • perfect_power_count_approx(n) โ€” fast approximate perfect power count
  • nth_perfect_power(n) โ€” the *n*th perfect power, periodt โœจ
  • nth_perfect_power_lower(n) โ€” fast lower bound for the nth perfect power
  • nth_perfect_power_upper(n) โ€” fast upper bound for the nth perfect power
  • nth_perfect_power_approx(n) โ€” fast approximate nth perfect power
  • next_chen_prime(n) โ€” the next Chen prime > n
  • smooth_count(n,k) โ€” count of k-smooth numbers <= n
  • rough_count(n,k) โ€” count of k-rough numbers <= n
  • powerfree_count(n[,k]) โ€” count of k-powerfree numbers <= n
  • nth_powerfree(n[,k]) โ€” the nth k-powerfree number, slay
  • powerfree_sum(n[,k]) โ€” sum of the k-powerfree numbers <= n
  • powerfree_part(n[,k]) โ€” strips the excess powers so n comes out k-free
  • powerfree_part_sum(n[,k]) โ€” sum of the k-powerfree parts for 1 to n
  • squarefree_kernel(n) โ€” the integer radical of |n|
  • powersum(n,k) โ€” sum of the kth powers from 1 to n ๐ŸŒธ

โœจ RATIONALS โœจ

  • contfrac(n,d) โ€” the continued fraction for n/d, as a list
  • from_contfrac(@A) โ€” gives you back the (p,q) rational from a cfrac list
  • next_calkin_wilf(n,d) โ€” next breadth-first CW rational
  • next_stern_brocot(n,d) โ€” next breadth-first SB rational
  • calkin_wilf_n(n,d) โ€” the index of a breadth-first CW rational
  • stern_brocot_n(n,d) โ€” the index of a breadth-first SB rational
  • nth_calkin_wilf(n) โ€” the CW rational sitting at breadth-first index n
  • nth_stern_brocot(n) โ€” the SB rational sitting at breadth-first index n
  • nth_stern_diatomic(n) โ€” Stern's Diatomic series; fusc(n) ๐Ÿ’œ
  • farey(n) โ€” the Farey sequence of order n, listed out
  • farey(n,k) โ€” the k'th entry of the Farey sequence of order n
  • next_farey(n,[p,q]) โ€” the next order-n rational after p/q
  • farey_rank(n,[p,q]) โ€” how many things in F_n are less than p/q

๐Ÿฆ‹ NON-INTEGER MATH ๐Ÿฆ‹

  • ExponentialIntegral(x) โ€” Ei(x) ๐Ÿ‘‘
  • LogarithmicIntegral(x) โ€” li(x)
  • RiemannZeta(x) โ€” ฮถ(s)-1, the real-valued Riemann Zeta
  • RiemannR(x) โ€” Riemann's R function
  • LambertW(k) โ€” Lambert W: solve for W in k = W exp(W)
  • Pi([n]) โ€” the constant ฯ€, either as an NV or as n digits, your call ๐Ÿ’…

๐ŸŒธ SUPPORT ๐ŸŒธ

  • prime_get_config โ€” hands you a hash ref of the current settings
  • prime_set_config(%hash) โ€” sets those parameters
  • prime_memfree โ€” frees up any cached memory, bye girl ๐Ÿซถ

๐Ÿ’… ADDITIONAL NON-EXPORTED ๐Ÿ’…

  • trial_factor(n[,limit]) โ€” factor using only trial division
  • fermat_factor(n) โ€” factor using only Fermat's method
  • holf_factor(n[,rounds]) โ€” factor using only Hart's OLF
  • lehman_factor(n) โ€” factor using only Lehman (limited size, bestie, it has a ceiling)
  • squfof_factor(n[,rounds]) โ€” factor using only SQUFOF
  • prho_factor(n[,rounds]) โ€” factor using only Pollard's Rho
  • pbrent_factor(n[,rounds]) โ€” factor using only Brent/Pollard Rho
  • pminus1_factor(n[,B1[,B2]]) โ€” factor using only P-1
  • pplus1_factor(n[,B]) โ€” factor using only P+1
  • cheb_factor(n[,B1[,initx]]) โ€” factor using only Chebyshev
  • ecm_factor(n[,B1[,B2[,curves]]]) โ€” factor using only ECM ๐Ÿ”ฅ
  • _uvbits โ€” size of UV in bits
  • _uvsize โ€” size of UV in bytes
  • _ivsize โ€” size of IV in bytes
  • _nvsize โ€” size of NV in bytes
  • _nvmantbits โ€” bits stored in the NV mantissa
  • _nvmantdigits โ€” count of whole decimal digits in an NV

๐Ÿงš ADDITIONAL NON-EXPORTED C ONLY ๐Ÿงš

(yeah โ€” these ones are C only, no Perl for you ๐Ÿ™ˆ)

  • _segment_pi(n) โ€” prime count using only sieving
  • _legendre_pi(n) โ€” prime count with the Legendre method
  • _meissel_pi(n) โ€” prime count with the Meissel method
  • _lehmer_pi(n) โ€” prime count with the Lehmer method
  • _LMO_pi(n) โ€” prime count with the LMO method
  • _LMOS_pi(n) โ€” prime count with the extended LMO method

Copyright 2011-2026 by Dana Jacobsen <dana@acm.org>

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page, bestie, we made it ๐Ÿ’– this is ntheory(3pm) as it ships with perl v5.42.3, dated 2026-08-20 ๐ŸŒˆ go forth and factor something iconic โœจ๐ŸŽ€

๐Ÿ“ฆ where this page comes from