Skip to content

FactorInteger ​

The prime factorisation of n as a list of [prime, exponent] pairs.

✓ checked against that system · ! checked, with disagreements (hover a chip)WikipediaInteger factorizationMathWorldPrimeFactorizationWikidataQ4846249Wolfram LanguageFactorInteger!Rosetta CodePrime decomposition
FactorInteger(n)prime factorization of nn as [prime,exponent][\mathrm{prime}, \mathrm{exponent}] pairs.
SageMathenumeratio_factor_integer(n)SymPylist(factorrat(S(n)).items())Juliaenumeratio_factor_integer(n)Rustfactor_integer(n)
FactorInteger(n, Over -> GaussianIntegers)the factorisation in Z[i]\mathbb{Z}[i]: a unit first when it is not 1, then first-quadrant primes; a complex nn is always factored there
FactorInteger(n, Over -> QuadraticIntegers(d))the prime factorisation of nn in the ring of integers of Q(d)\mathbb{Q}(\sqrt d), where that ring factors uniquely
SageMathenumeratio_factor_integer(d)SymPylist(factorrat(S(d)).items())Juliaenumeratio_factor_integer(d)Rustfactor_integer(d)
Typed signatures
TypePackageApplies
(integer) -> list<tuple<integer, integer>>compute-enginealways
(number | quadratic_integer, any*) -> listnumber-theoryreplaces compute-enginealways

Domain: Number theory

Details
  • For n=p1k1⋯pmkmn=p_1^{k_1}\cdots p_m^{k_m}, returns the pairs {p1,k1},…,{pm,km}\{p_1,k_1\},\ldots,\{p_m,k_m\} in increasing order of prime.
  • Multiplying the pep^e factors back together recovers nn.
  • compute-engine represents 1 as 111^1 and 0 as 010^1.
  • Negative nn carries an explicit −11-1^1 unit factor ahead of the prime factors.
  • compute-engine handles ordinary integers only.

Examples

28 more kept as data, checked against the oracles
Basic #
live ↗
InFactorInteger(360)\mathrm{FactorInteger}(360)Out[(2,3),(3,2),(5,1)]\bigl\lbrack(2,3), (3,2), (5,1)\bigr\rbrack
live ↗
InFactorInteger(84)\mathrm{FactorInteger}(84)Out[(2,2),(3,1),(7,1)]\bigl\lbrack(2,2), (3,1), (7,1)\bigr\rbrack
live ↗
InFactorInteger(120)\mathrm{FactorInteger}(120)Out[(2,3),(3,1),(5,1)]\bigl\lbrack(2,3), (3,1), (5,1)\bigr\rbrack
live ↗
InFactorInteger(36)\mathrm{FactorInteger}(36)Out[(2,2),(3,2)]\bigl\lbrack(2,2), (3,2)\bigr\rbrack
Scope #

Threads element-wise over a list, as Wolfram's Listable heads do

live ↗
InFactorInteger([12,18])\mathrm{FactorInteger}(\bigl\lbrack12, 18\bigr\rbrack)Out[[(2,2),(3,1)],[(2,1),(3,2)]]\bigl\lbrack\bigl\lbrack(2,2), (3,1)\bigr\rbrack, \bigl\lbrack(2,1), (3,2)\bigr\rbrack\bigr\rbrack

5=−i(1+2i)(2+i)5 = -i(1 + 2i)(2 + i)

live ↗
InFactorInteger(5,Rule(Over,GaussianIntegers))\mathrm{FactorInteger}(5, \mathrm{Rule}(\mathrm{Over}, \mathrm{GaussianIntegers}))Out[(−i,1),(1+2i,1),(2+i,1)]\bigl\lbrack(-i,1), (1+2i,1), (2+i,1)\bigr\rbrack

3+4i=(2+i)23 + 4i = (2 + i)^2

live ↗
InFactorInteger(3+4i)\mathrm{FactorInteger}(3+4i)Out[(2+i,2)]\bigl\lbrack(2+i,2)\bigr\rbrack

a 21-digit Gaussian integer, through its norm

live ↗
InFactorInteger(100 000 000 000 000 000 039+100 000 000 000 000 000 129i)\mathrm{FactorInteger}(100\,000\,000\,000\,000\,000\,039+100\,000\,000\,000\,000\,000\,129i)Out[(−i,1),(1+i,1),(99+34i,1),(1 538+213i,1),(277 789 996 706 096+549 000 467 740 335i,1)]\bigl\lbrack(-i,1), (1+i,1), (99+34i,1), (1\,538+213i,1), (277\,789\,996\,706\,096+549\,000\,467\,740\,335i,1)\bigr\rbrack

Rationals: the denominator's primes get negative exponents

live ↗
InFactorInteger(38)\mathrm{FactorInteger}(\frac{3}{8})Out[(2,−3),(3,1)]\bigl\lbrack(2,-3), (3,1)\bigr\rbrack

9+12i=3(2+i)29+12i=3(2+i)^2

live ↗
InFactorInteger(9+12i)\mathrm{FactorInteger}(9+12i)Out[(2+i,2),(3,1)]\bigl\lbrack(2+i,2), (3,1)\bigr\rbrack

36=−(1+i)4⋅3236=-(1+i)^4\cdot3^2: 2 ramifies, 3 stays prime

live ↗
InFactorInteger(36,Rule(Over,GaussianIntegers))\mathrm{FactorInteger}(36, \mathrm{Rule}(\mathrm{Over}, \mathrm{GaussianIntegers}))Out[(−1,1),(1+i,4),(3,2)]\bigl\lbrack(-1,1), (1+i,4), (3,2)\bigr\rbrack

Listable: threads over a list

live ↗
InFactorInteger([11,101,1 001])\mathrm{FactorInteger}(\bigl\lbrack11, 101, 1\,001\bigr\rbrack)Out[[(11,1)],[(101,1)],[(7,1),(11,1),(13,1)]]\bigl\lbrack\bigl\lbrack(11,1)\bigr\rbrack, \bigl\lbrack(101,1)\bigr\rbrack, \bigl\lbrack(7,1), (11,1), (13,1)\bigr\rbrack\bigr\rbrack

A 21-digit prime cubed factors at once

live ↗
InFactorInteger(100 000 000 000 000 000 0393)\mathrm{FactorInteger}(100\,000\,000\,000\,000\,000\,039^3)Out[(100 000 000 000 000 000 039,3)]\bigl\lbrack(100\,000\,000\,000\,000\,000\,039,3)\bigr\rbrack
Properties #

Multiplying the pep^e factors back together recovers nn: 23⋅32⋅5=3602^3\cdot3^2\cdot5=360

live ↗
In23×32×5=3602^3×3^2×5=360OutTrue\mathrm{True}
Possible issues #

compute-engine returns the trivial factor 111^1

live ↗
InFactorInteger(1)\mathrm{FactorInteger}(1)Out[(1,1)]\bigl\lbrack(1,1)\bigr\rbrack

0 is represented as 010^1

live ↗
InFactorInteger(0)\mathrm{FactorInteger}(0)Out[(0,1)]\bigl\lbrack(0,1)\bigr\rbrack

Negative numbers carry an explicit −1-1 unit factor

live ↗
InFactorInteger(−60)\mathrm{FactorInteger}(-60)Out[(−1,1),(2,2),(3,1),(5,1)]\bigl\lbrack(-1,1), (2,2), (3,1), (5,1)\bigr\rbrack

−1-1 is a unit and factors as itself

live ↗
InFactorInteger(−1)\mathrm{FactorInteger}(-1)Out[(−1,1)]\bigl\lbrack(-1,1)\bigr\rbrack
Neat examples #

The Hardy-Ramanujan taxicab number, 7×13×197\times13\times19. See Divisors

live ↗
InFactorInteger(1 729)\mathrm{FactorInteger}(1\,729)Out[(7,1),(13,1),(19,1)]\bigl\lbrack(7,1), (13,1), (19,1)\bigr\rbrack
About
Examples47
Checked againstWolfram Language 45 agree2 differSageMath 33 agree3 differSymPy 11 agree7 differ4 errormathlib4 6 agree3 differ13 errorJulia 18 agree19 errorRust 8 agree2 differ12 errormpmath 1 agreeOscar 1 agree
CrosswalkWikipediaMathWorldWikidataWolfram LanguageSageMathSymPyJuliaRustRosetta Code

Implementation

mappedsagepackages/oracle/src/mappings.ts

Sage has no unit factor for 1, 0 or a negative n — a shape difference from compute-engine's explicit 1^1/0^1/-1^1, not a bug.

mappedsympypackages/oracle/src/mappings.ts

factorrat is SymPy's factorint that also takes a rational, giving the denominator's primes negative exponents as ours does. SymPy has no factor for 1, writes the -1 of a negative integer after the primes and a negative rational's between the numerator's primes and the denominator's, and lists factors in the order it finds them, which for a large n is not always ascending. A Complex operand is not emitted: factorrat takes rationals only, and SymPy has no factorization in Z[i] to ask for.

mappedsympypackages/oracle/src/mappings.ts

The Over -> GaussianIntegers and QuadraticIntegers(d) forms have no SymPy counterpart: factorint and factorrat are over Z and Q, GaussianIntegerRing has a gcd but no prime factorization, and factor(n, gaussian=True) factors polynomials, leaving an integer as it is. Composing one from factorint of the norm and a gcd would test our algorithm against a restatement of itself.

mappedjuliapackages/oracle/src/mappings.ts
mappedrustpackages/oracle/src/mappings.ts

Positive operands only.

mappedmathlib4packages/oracle/src/mappings.ts

Nat.primeFactorsList (ascending, with multiplicity) grouped into [prime, exponent] pairs. Natural n only: Mathlib has no unit factor, so 0 and 1 give [] where ours gives 0^1 and 1^1, and a negative or rational n is a domain error.

See also: NthPrime, Divisors, PrimeNu, PrimeOmega