factor
Prime factorization of a positive integer.
Category: Mathematics > Elementary Math
Syntax
p = factor(n)
Returns the prime factors of n in a row vector, in nondecreasing order, repeated according to multiplicity.
Description
The factorization is found by trial division, so it is quick for numbers that fit in a double (up to 2^53) and slower when n has two large prime factors. Taking prod(factor(n)) always recovers n.
To see each prime once together with its exponent, apply unique to the result and count with histc or accumarray.
Input arguments
n- Nonnegative integer scalar. Integer classes are accepted and the result keeps the class.
Output arguments
p- Row vector of primes whose product is
n. For a primenit isnitself; for1it is empty.
Example
Factor and rebuild
p = factor(360)
prod(p)
factor(97)
Output
p = 1×6
2 2 2 3 3 5
ans = 360
ans = 97
Notes
The two-output form [p, k] = factor(n) is an error in MATLAB as well ("Too many output arguments"). To get each prime once with its exponent, apply unique to the result.