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 importrand,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)โ is2^p-1prime or composite. ๐งis_aks_prime(n)โ AKS deterministic test (slow). ๐ she said what she saidis_ramanujan_prime(n)โ isna Ramanujan prime. ๐is_gaussian_prime(a,b)โ isa+bia 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=1returns2). ๐ฅ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=1returns2). ๐ฅ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=1returns3). ๐ฅ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 forRn. โฌ๏ธnth_ramanujan_prime_upper(n)โ fast upper bound forRn. โฌ๏ธnth_ramanujan_prime_approx(n)โ fast approximateRn. ใฐ๏ธ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 ton. ๐งsum_primes([start,] end)โ return summation of primes in range. โprint_primes(start,end[,fd])โ print primes to stdout orfd. ๐จ๏ธ
๐งฉ FACTORING ๐งฉ
factor(n)โ array of prime factors ofn. ๐งฉfactor_exp(n)โ array of[p,k]factorsp^k. ๐งฎdivisors(n)โ array of divisors ofn. ๐ฐ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-freen. ๐forsquarefreeint {...} [start,] endโ loop over square-freen. ๐fordivisors { ... } nโ loop over the divisors ofn. ๐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 offor....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 withnrandom bytes. ๐ขentropy_bytes(n)โ string withnentropy-source bytes. ๐ช๏ธurandomb(n)โ random integer less than2^n. ๐ฒurandomm(n)โ random integer less thann. ๐ฒcsrand(data)โ seed the CSPRNG with binary data. ๐ฑsrand([seed])โ simple seed (exported with:rand). ๐ฑrand([limit])โ alias fordrand(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 withndigits. ๐ขrandom_nbit_prime(n)โ random prime withnbits. ๐ขrandom_safe_prime(n)โ random safe prime withnbits. ๐ก๏ธrandom_strong_prime(n)โ random strong prime withnbits. ๐ช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]])โ turnsninto an array of its digits in whatever base you wanttodigitstring(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 numberfromdigits(str,[,base])โ takes a digit string and converts it back into a number โจsumdigits(n)โ adds up all the digits ofn, and yes there's an optional basetozeckendorf(n)โ convertsninto Zeckendorf / Fibbinary form ๐ฆfromzeckendorf(str)โ the glow-up in reverse: a Zeckendorf binary string becomes a number againis_odd(n)โ returns 1 ifnis odd, 0 otherwiseis_even(n)โ returns 1 ifnis even, 0 otherwiseis_divisible(n,d)โ returns 1 ifnis divisible bydis_congruent(n,c,d)โ returns 1 ifnis congruent tocmoddis_qr(a,n)โ returns 1 ifais a quadratic residue modnis_square(n)โ returns 1 ifnis a perfect square ๐is_power(n)โ returnskifn = c^kfor some integercis_power(n,k)โ returns 1 ifn = c^kfor an integercand that specifickis_power(n,k,\$root)โ same as above but it also dropscinto$rootfor youis_perfect_power(n)โ returns 1 ifn = c^kwherec != 0andk > 1is_prime_power(n)โ returnskifn = p^kfor a primepandk > 0is_prime_power(n,\$p)โ same deal but it also sets$ptop๐is_square_free(n)โ true when no prime factor shows up more than once (no repeated factors)is_powerfree(n[,k])โ isnfree of anyk-th powers?is_cyclic(n)โ doesnhave only one group of ordern?is_carmichael(n)โ isna Carmichael number?is_quasi_carmichael(n)โ isna quasi-Carmichael number?is_primitive_root(r,n)โ isra primitive root modn?is_pillai(n)โ gives youvwherev! % n == n-1andn % v != 1is_semiprime(n)โ doesnhave exactly 2 prime factors? ๐is_almost_prime(k,n)โ doesnhave exactlykprime factors?is_omega_prime(k,n)โ isndivisible by exactlykprimes?is_chen_prime(n)โ isnprime andn+2prime or semiprime?is_polygonal(n,k)โ isnak-polygonal number?is_polygonal(n,k,\$root)โ same, but it also sets$rootfor you โจis_sum_of_squares(n[,k])โ isna sum ofksquares (default is 2)?is_congruent_number(n)โ isna congruent number?is_perfect_number(n)โ isnequal to the sum of its proper divisors? iconic behavior tbhis_fundamental(d)โ isda fundamental discriminant?is_totient(n)โ isn = euler_phi(x)for somex?is_lucky(n)โ isna lucky number? ๐is_happy(n)โ ifnis a happy number it returns the height ๐is_happy(n,base,exponent)โ same but forS_b_ehappy numbers, returns the heightis_smooth(n,k)โ isnak-smooth number?is_rough(n,k)โ isnak-rough number?is_powerful(n[,k])โ isnak-powerful number?is_practical(n)โ isna practical number?is_delicate_prime(n)โ isna digitally delicate prime? ๐powint(a,b)โ signed integera^bmulint(a,b)โ signed integera * baddint(a,b)โ signed integera + bsubint(a,b)โ signed integera - badd1int(n)โ signed integern + 1sub1int(n)โ signed integern - 1divint(a,b)โ signed integera / b(floor)modint(a,b)โ signed integera % b(floor)cdivint(a,b)โ signed integera / b(ceilint)divrem(a,b)โ returns(quot,rem)ofa/b(Euclidian)fdivrem(a,b)โ returns(quot,rem)ofa/b(floored)cdivrem(a,b)โ returns(quot,rem)ofa/b(ceiling)tdivrem(a,b)โ returns(quot,rem)ofa/b(truncated)lshiftint(n,k)โ left shiftnbykbitsrshiftint(n,k)โ right shiftnbykbits (truncate)rashiftint(n,k)โ right shiftnbykbits (floor)absint(n)โ integer absolute valuenegint(n)โ integer negationcmpint(a,b)โ integer comparison, works like<=>signint(n)โ integer sign as-1,0, or1sqrtint(n)โ integer square rootrootint(n,k)โ integerk-th rootrootint(n,k,\$rk)โ same but it also sets$rktor^k๐ซถlogint(n,b)โ integer logarithmlogint(n,b,\$be)โ same but it also sets$betob^egcd(@list)โ greatest common divisorlcm(@list)โ least common multiplegcdext(x,y)โ returns(u,v,d)whereu*x+v*y=dchinese([a,mod1],[b,mod2],...)โ CRT that returns the remainderchinese2([a,mod1],[b,mod2],...)โ CRT that returns(remainder,LCM)frobenius_number(@list)โ the Frobenius Number of a setprimorial(n)โ product of all primes belownpn_primorial(n)โ product of the firstnprimesfactorial(n)โ product of the firstnintegers, akan!factorialmod(n,m)โ factorial modmsubfactorial(n)โ count of derangements ofnobjectsbinomial(n,k)โ binomial coefficientbinomialmod(n,k,m)โbinomial(n,k)modmfalling_factorial(x,n)โ falling factorialrising_factorial(x,n)โ rising factorialpartitions(n)โ number of integer partitionsvaluation(n,k)โ how many timesnis divisible bykhammingweight(n)โ population count, aka the number of binary 1skronecker(a,b)โ Kronecker (Jacobi) symbolnegmod(a,n)โ-a mod naddmod(a,b,n)โa + b mod nsubmod(a,b,n)โa - b mod nmulmod(a,b,n)โa * b mod nmuladdmod(a,b,c,n)โa * b + c mod nmulsubmod(a,b,c,n)โa * b - c mod ndivmod(a,b,n)โa / b mod npowmod(a,b,n)โa ^ b mod n๐ฅinvmod(a,n)โ inverse ofamodulonsqrtmod(a,n)โ modular square rootrootmod(a,k,n)โ modulark-th rootallsqrtmod(a,n)โ list of all the modular square rootsallrootmod(a,k,n)โ list of all the modulark-th rootscornacchia(d,n)โ findsx,yforx^2 + d * y^2 = nprime_bigomega(n)โ number of prime factorsprime_omega(n)โ number of distinct prime factorsmoebius(n)โ Moebius function ofnmoebius(beg, end)โ list of Moebius values over a rangemertens(n)โ sum of Moebius from 1 toneuler_phi(n)โ Euler totient ofneuler_phi(beg, end)โ Euler totient over a rangeinverse_totient(n)โ the image of Euler totientjordan_totient(k,n)โ Jordan's totientsumtotient(n)โ sum of Euler totient from 1 toncarmichael_lambda(n)โ Carmichael's Lambda functionramanujan_sum(k,n)โ Ramanujan's sumexp_mangoldt(n)โ exponential of the Mangoldt functionliouville(n)โ Liouville functionsumliouville(n)โ sum of Liouville from 1 tonznorder(a,n)โ multiplicative order ofamodnznprimroot(n)โ smallest primitive rootznlog(a, g, p)โ solves forkina = g^k mod pqnr(n)โ least quadratic non-residuechebyshev_theta(n)โ first Chebyshev functionchebyshev_psi(n)โ second Chebyshev functionhclassno(n)โ Hurwitz class numberH(n) * 12ramanujan_tau(n)โ Ramanujan's Tau functionconsecutive_integer_lcm(n)โlcm(1 .. n)lucasu(P, Q, k)โU_kforLucas(P,Q)lucasv(P, Q, k)โV_kforLucas(P,Q)lucasuv(P, Q, k)โ(U_k,V_k)forLucas(P,Q)lucasumod(P, Q, k, n)โU_kforLucas(P,Q)modnlucasvmod(P, Q, k, n)โV_kforLucas(P,Q)modnlucasuvmod(P, Q, k, n)โ(U_k,V_k,Q^k)forLucas(P,Q)modnlucas_sequence(n,P,Q,k)โ deprecated, please uselucasuvmodinstead bestie, fr frpisano_period(n)โ the period of Fibonacci numbers modnbernfrac(n)โ Bernoulli number as(num,den)bernreal(n)โ Bernoulli number as a BigFloatharmfrac(n)โ Harmonic number as(num,den)harmreal(n)โ Harmonic number as a BigFloatstirling(n,m,[type])โ Stirling numbers, 1st or 2nd type, your pick โจfubini(n)โ Fubini (Ordered Bell) numbernumtoperm(n,k)โ thek-th lexico permutation ofnelementspermtonum([a,b,...])โ permutation number of the permutation you feed itrandperm(n,[k])โ random permutation ofnelements ๐ฒshuffle(...)โ random permutation of an arraylucky_numbers(n)โ array ref of the lucky sieve up tonlucky_count(n)โ count of lucky numbers<= nlucky_count(start, end)โ count of lucky numbers in a rangelucky_count_lower(n)โ fast lower bound for the lucky countlucky_count_upper(n)โ fast upper bound for the lucky countlucky_count_approx(n)โ fast approximate lucky countnth_lucky(n)โ then-th entry in the lucky sieventh_lucky_lower(n)โ fast lower bound for then-th lucky numbernth_lucky_upper(n)โ fast upper bound for then-th lucky numbernth_lucky_approx(n)โ fast approximaten-th lucky numberminimal_goldbach_pair(n)โ the least primepwheren-pis also primegoldbach_pair_count(n)โ how many prime pairs sum tongoldbach_pairs(n)โ array of everypwherepandn-pare both primepowerful_numbers([lo,]hi[,k])โ array ref ofk-powerful numbers fromlotohipowerful_count(n[,k])โ count ofk-powerful numbers<= nsumpowerful(n[,k])โ sum ofk-powerful numbers<= nnth_powerful(n[,k])โ then-thk-powerful numbernext_perfect_power(n)โ the next perfect power greater thann๐
prev_perfect_power(n)โ the perfect power that comes right before< n๐perfect_power_count(n)โ how many perfect powers are<= nperfect_power_count(start, end)โ how many live inside that rangeperfect_power_count_lower(n)โ fast lower bound for the perfect power countperfect_power_count_upper(n)โ fast upper bound for the perfect power countperfect_power_count_approx(n)โ fast approximate perfect power countnth_perfect_power(n)โ the *n*th perfect power, periodt โจnth_perfect_power_lower(n)โ fast lower bound for the nth perfect powernth_perfect_power_upper(n)โ fast upper bound for the nth perfect powernth_perfect_power_approx(n)โ fast approximate nth perfect powernext_chen_prime(n)โ the next Chen prime> nsmooth_count(n,k)โ count of k-smooth numbers<= nrough_count(n,k)โ count of k-rough numbers<= npowerfree_count(n[,k])โ count of k-powerfree numbers<= nnth_powerfree(n[,k])โ the nth k-powerfree number, slaypowerfree_sum(n[,k])โ sum of the k-powerfree numbers<= npowerfree_part(n[,k])โ strips the excess powers so n comes out k-freepowerfree_part_sum(n[,k])โ sum of the k-powerfree parts for 1 to nsquarefree_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 forn/d, as a listfrom_contfrac(@A)โ gives you back the(p,q)rational from a cfrac listnext_calkin_wilf(n,d)โ next breadth-first CW rationalnext_stern_brocot(n,d)โ next breadth-first SB rationalcalkin_wilf_n(n,d)โ the index of a breadth-first CW rationalstern_brocot_n(n,d)โ the index of a breadth-first SB rationalnth_calkin_wilf(n)โ the CW rational sitting at breadth-first index nnth_stern_brocot(n)โ the SB rational sitting at breadth-first index nnth_stern_diatomic(n)โ Stern's Diatomic series;fusc(n)๐farey(n)โ the Farey sequence of order n, listed outfarey(n,k)โ the k'th entry of the Farey sequence of order nnext_farey(n,[p,q])โ the next order-n rational afterp/qfarey_rank(n,[p,q])โ how many things inF_nare less thanp/q
๐ฆ NON-INTEGER MATH ๐ฆ
ExponentialIntegral(x)โEi(x)๐LogarithmicIntegral(x)โli(x)RiemannZeta(x)โฮถ(s)-1, the real-valued Riemann ZetaRiemannR(x)โ Riemann's R functionLambertW(k)โ Lambert W: solve for W ink = 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 settingsprime_set_config(%hash)โ sets those parametersprime_memfreeโ frees up any cached memory, bye girl ๐ซถ
๐ ADDITIONAL NON-EXPORTED ๐
trial_factor(n[,limit])โ factor using only trial divisionfermat_factor(n)โ factor using only Fermat's methodholf_factor(n[,rounds])โ factor using only Hart's OLFlehman_factor(n)โ factor using only Lehman (limited size, bestie, it has a ceiling)squfof_factor(n[,rounds])โ factor using only SQUFOFprho_factor(n[,rounds])โ factor using only Pollard's Rhopbrent_factor(n[,rounds])โ factor using only Brent/Pollard Rhopminus1_factor(n[,B1[,B2]])โ factor using only P-1pplus1_factor(n[,B])โ factor using only P+1cheb_factor(n[,B1[,initx]])โ factor using only Chebyshevecm_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 ๐
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
- ubuntu stonking: libmath-prime-util-perl 0.74-1build1 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms