ifactor
Factor an integer into primes
MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.
MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.
ifactor(n
, <UsePrimeTab
>) ifactor(<PrimeLimit>)
ifactor(n)
computes the prime factorization n = s p_{1}^{e1} … p_{r}^{er} of
the integer n,
where s is
the sign of n, p_{1},
…, p_{r} are
the distinct positive prime divisors of n,
and e_{1},
…, e_{r} are
positive integers.
The result of ifactor
is an object of domain
type Factored
.
Let f:= ifactor(n)
be such an object. Internally,
it is represented by the list[s,
p1, e1, ..., pr, er]
of odd length 2 r +
1, where r is
the number of distinct prime divisors of n.
The p_{i} are
not necessarily sorted by magnitude.
You may extract the sign s and
the terms p_{i}^{ei} by
means of the index operator [ ]
, i.e., f[1] = p1^e1, f[2]
= p2^e2, ...
for positive n and f[1]
= s, f[2] = p1^e1, f[3] = p2^e2, ...
for negative n.
The call Factored::factors(f)
yields a list
of the factors [p1, p2, ...]
, while Factored::exponents(f)
returns
a list of the exponents [e1, e2, ...]
with 1
≤ i ≤ r.
The factorization of 0, 1,
and  1 yields the single
factor 0, 1,
and  1, respectively. In
these cases, the internal representation is the list [0]
, [1]
,
and [1]
, respectively.
The call coerce(f,DOM_LIST)
returns the internal
representation of a factored object, i.e., the list [s, p1,
e1, p2, e2, ...]
.
Note that the result of ifactor
is printed
as an expression, and it is implicitly converted into an expression
whenever it is processed further by other MuPAD^{®} functions. For
example, the result of ifactor(12)
is printed as 2^2*3
,
which is an expression of type "_mult"
.
See Example 1 for illustrations,
and the help page of Factored
for more details.
If you do not need the prime factorization of n
,
but only want to know whether it is composite or prime, use isprime
instead, which
is much faster.
ifactor
returns an error when the argument
is a number but not an integer. A symbolic ifactor
call
is returned if the argument is not a number.
To get the prime factorization of 120, enter:
f := ifactor(120)
You can access the terms of this factorization using the index operator:
f[1], f[2], f[3]
The internal representation of f
, namely
the list as described above, is returned by the following command:
coerce(f, DOM_LIST)
The result of ifactor
is an object of domain
type Factored
:
domtype(f)
This domain implements some features for handling such objects. Some of them are described below.
You may extract the factors and exponents of the factorization also in the following way:
Factored::factors(f), Factored::exponents(f)
You can ask for the type of the factorization:
Factored::getType(f)
This output means that all factors p_{i} are
prime. Other possible types are "squarefree"
(see polylib::sqrfree
) or "unknown"
.
Multiplying factored objects preserves the factored form:
f2 := ifactor(12)
f*f2
It is important to note that you can apply nearly any function
operating on arithmetical expressions to an object of domain type Factored
.
The result is usually not of this domain type:
expand(f); domtype(%)
For a detailed description of these objects, please refer to
the help page of the domain Factored
.
The factorizations of 0
, 1
,
and 1
each have exactly one factor:
ifactor(0), ifactor(1), ifactor(1)
map(%, coerce, DOM_LIST)
The internal representation of the factorization of a prime
number p
is the list [1, p, 1]
:
coerce(ifactor(5), DOM_LIST)
The bound on the prime number table is:
ifactor(PrimeLimit)
We assign a large prime number to p
:
p := nextprime(10^10); q := nextprime(10^12)
Completely factoring the 36
digit number 6*p^3
takes
some time; the second output line shows the time in seconds:
t := time(): f := ifactor(p^3*q^4); (time()  t)/1000.0
10000000019^3*1000000000039^4
2.5
Factored::getType(f)
"irreducible"
delete f
Extracting only the prime factors in the prime table is much faster, but it does not yield the complete factorization; the factor p^{3} remains undecomposed:
t := time(): f := ifactor(p^3*q^4, UsePrimeTab); (time()  t)/1000.0
1000000005856000011728326008600735477170193366706178119695352530650045867891819
0.015625
Factored::getType(f)
"unknown"
delete f

An arithmetical expression representing an integer 

Internally, MuPAD has stored a precomputed table of all
prime numbers up to a certain bound. 

The size of this table can be changed via the MuPAD command
line flag 
Object of domain type Factored
, or a symbolic ifactor
call.
ifactor
uses the elliptic curve method.
ifactor
is an interface to the kernel function stdlib::ifactor
.
It calls stdlib::ifactor
with the given arguments
and convert its result, which is the list [s, p1, e1, ...,
pr, er]
as described above, into an object of the domain
type Factored
.
You may directly call the kernel function stdlib::ifactor
inside
your routines, in order to avoid this conversion and to decrease the
running time.