LinuxCommandLibrary

factor

Find prime factors of an integer

TLDR

Display the prime-factorization of a number

$ factor [number]
copy

Take the input from stdin if no argument is specified
$ echo [number] | factor
copy

SYNOPSIS

factor [OPTION]... [NUMBER]...

PARAMETERS

--help
    display this help and exit

--version
    output version information and exit

DESCRIPTION

The factor command, part of GNU Coreutils, decomposes specified positive integers into their prime factors using trial division. It divides each number by primes up to its square root, listing factors with multiplicity in ascending order.

Invoke with arguments like factor 42, outputting 42: 2 3 7. For primes like 17, it shows 17: 17. With multiple inputs, e.g., factor 12 32:
12: 2 2 3
32: 2 2 2 2 2

If no arguments, reads positive integers from stdin, one per line. Useful for math education, scripting number theory tasks, primality checks (primes factor as themselves), or verifying factorizations.

Handles 1 as 1: (no primes). Invalid inputs (0, negatives, non-integers) error with "Not a valid integer".

Efficient for small/medium numbers (<10^10), but slows for larger due to O(sqrt(n)) complexity—no advanced methods like Pollard's rho.

CAVEATS

factor inefficient for large numbers (>10^12) due to trial division; limited to positive decimal integers; errors on invalid input like 0 or negatives.

OUTPUT FORMAT

Each line: NUMBER: followed by space-separated primes (with repeats for multiplicity), sorted ascending.
Empty for 1: 1:

EXAMPLES

factor 100
100: 2 2 5 5
echo 315 | factor
315: 3 3 5 7
factor (reads stdin)

HISTORY

Originated in early Unix (4.3BSD-Tahoe, 1986); integrated into GNU Coreutils (1990s) by GNU Project; POSIX-specified; evolves with coreutils for portability and bug fixes.

SEE ALSO

bc(1), dc(1), awk(1)

Copied to clipboard