Home / Alt manpages / factor(1)

  • factor(1)
  • User command
  • linux

Factor Integers Safely with GNU factor

You will use GNU factor to print the prime factors of integers, switch between expanded and exponent notation, and process a list from standard input. Allow about ten minutes. You need a shell and the coreutils package; no elevated privileges are needed.

The examples match GNU coreutils 9.4, installed here as Ubuntu package version 9.4-3ubuntu6.3. Factorisation can take longer for very large difficult inputs, so start with a small value when checking a script.

1. Confirm the installed command

Check which executable your shell will run, then record its version:

$ command -v factor
/usr/bin/factor
$ factor --version | head -n 1
factor (GNU coreutils) 9.4

The command belongs to GNU coreutils. If command -v finds nothing, install coreutils through your operating system's normal package manager. Do not use sudo for factorisation itself. The program only reads its arguments or standard input and writes results to standard output.

Checkpoint: the version output should identify GNU factor. If you are debugging a script on another host, record that host's version too, because package versions can differ.

2. Factor one or more numbers

Pass positive integers after the command. Each output line repeats the input, followed by its prime factors:

$ factor 360 17 1 0
360: 2 2 2 3 3 5
17: 17
1:
0:

The factors are printed in ascending order. A prime number is printed as itself. The lines for 1 and 0 contain no factor values, so do not interpret a missing value after the colon as a command failure. Check the exit status separately when a script needs to distinguish successful processing from invalid input:

$ factor 360 >/tmp/factor-output
$ status=$?
$ printf 'status=%s\n' "$status"
status=0
$ cat /tmp/factor-output
360: 2 2 2 3 3 5

The temporary output file is only for this example. For a result you need to keep, choose a destination in your working directory instead of relying on /tmp.

3. Compress repeated factors

Use -h or --exponents when repeated factors are easier to read as powers:

$ factor --exponents 360 17 1 0
360: 2^3 3^2 5
17: 17
1:
0:

The option only changes presentation. A factor with exponent one stays as a plain number, so 5 means 5^1 without printing the exponent. Use the long form in documentation and the short form when typing interactively:

$ factor -h 360
360: 2^3 3^2 5

Checkpoint: multiply the displayed values if you need to verify a result manually. For 360, 2^3 * 3^2 * 5 is 360.

4. Read a list from standard input

With no numbers on the command line, factor reads numbers from standard input. This makes it useful in a pipeline or for a short here-document:

$ printf '%s\n' 360 17 12 | factor
360: 2 2 2 3 3 5
17: 17
12: 2 2 3
$ factor --exponents <<'EOF'
360
100
EOF
360: 2^3 3^2 5
100: 2^2 5^2

Put one integer on each input line. The output preserves the input order. A pipeline can hide which command failed, so save the status when that matters:

if ! printf '%s\n' 360 100 | factor --exponents > factors.txt; then
    printf '%s\n' 'factor failed' >&2
    exit 1
fi
cat factors.txt

This writes a new file, but a shell redirection with > truncates an existing file before factor runs. If overwriting would lose useful data, select a new name such as factors.txt.new, check it, then replace the old file explicitly. To recover from a failed trial, remove only that known temporary file, for example rm -- factors.txt.new; never substitute a broad wildcard.

5. Handle invalid input without guessing

Factor expects positive integer input. Text containing other characters is rejected:

$ factor 12x
factor: '12x' is not a valid positive integer
$ printf '%s\n' "$?"
1

A leading minus is treated as an option unless it is otherwise accepted by the installed command, so do not feed signed values to a script that expects factor output. Validate and normalise input before calling factor if your source can contain signs, spaces, decimal points or labels. Do not parse the diagnostic text as a factor list: use the exit status and keep standard error separate from standard output.

For a batch, stop at the first rejected value if later output would be misleading:

while IFS= read -r number; do
    if ! factor --exponents -- "$number"; then
        printf 'invalid input: %s\n' "$number" >&2
        exit 1
    fi
done < numbers.txt

The -- separates options from the value. Even with that separator, the value still has to be a valid positive integer. Keep numbers.txt unchanged until the run has completed and its output has been checked.

6. Use help and version output when behaviour differs

The installed command documents its supported options locally:

$ factor --help
Usage: factor [OPTION] [NUMBER]...
Print the prime factors of each specified integer NUMBER.  If none
are specified on the command line, read them from standard input.

  -h, --exponents   print repeated factors in form p^e unless e is 1
      --help        display this help and exit
      --version     output version information and exit

If a script runs on several distributions, test the actual installed version rather than assuming that another implementation accepts the same syntax. Keep the input file and the original output until the new run has passed its own checks. Factor does not edit either file, and it does not require root.

Done means

  • factor --version identifies the implementation you tested.
  • Positive integers produce one labelled output line each.
  • --exponents is used when repeated factors should be compressed.
  • Standard input is one integer per line, and pipeline status is checked where it matters.
  • Invalid input is handled through the exit status, not mistaken for factor output.
  • No source file is changed, and any redirected replacement is checked before it replaces an existing result.