← All commands
THE COMMAND REFERENCE

factor

Print the prime factors of numbers.

Last updated

GNU

Implementation-level reference; examples are not certified for every installed version.

Overview

factor breaks a whole number into its prime factors, and prints the number, a colon and the factors. Without arguments it reads numbers from standard input, one per line. It is mostly a curiosity, but it is quick for checking whether a number is prime.

Background

factor is part of GNU coreutils and is also an applet in BusyBox. macOS does not ship it. They agree on the output format N: p1 p2 ....

Examples

Factor a number:

factor 12

Factor several numbers at once:

factor 12 17

Read the numbers from standard input:

echo 30 | factor

Check whether a number is prime: a prime has exactly one factor, itself:

factor 17 | awk '{print NF - 1}'

Common mistakes

A prime prints as 17: 17, which is easy to read as a mistake. Very large numbers can take a long time, and the limit differs between implementations. factor accepts only non-negative whole numbers.

Portability

This page has no Apple view: macOS has no factor. The Common examples were executed on GNU coreutils 9.11 and BusyBox 1.37.0 (see the development guide for the exact check).

About GNU factor

GNU factor is part of GNU coreutils, and this page's checks ran coreutils 9.11. It supports numbers larger than 64 bits when built with a big-number library, and coreutils 9.11 has -h for exponents; older versions may not support it.

GNU examples

Print repeated factors as powers with -h:

factor -h 12

Options

-h, --exponents
Print a repeated factor as p^e.

References

Original explanations, with upstream documentation for further reading. FreeBSD references are comparison material, not a claim of Apple version equivalence.