factor

Factor integers into prime factors.

Category: Fun / Math coreutils number theory primes utilities

Install

# Usually already installed (GNU coreutils)

# RHEL / Alma / Rocky
sudo dnf install coreutils

# Debian / Ubuntu
sudo apt install coreutils

# openSUSE
sudo zypper install coreutils

# Arch
sudo pacman -S coreutils

What it does

factor prints the prime factorization of each integer you give it. You can pass numbers on the command line, or pipe/redirect a list of numbers on stdin.

How it works (mechanical)

  • Parses each input as a non‑negative integer (0 and 1 are allowed).
  • Finds prime factors and prints them in nondecreasing order.
  • For large integers, it uses progressively heavier algorithms; very large semiprimes can take time.
  • Each input line becomes one output line: n: p1 p2 ....
  • Part of GNU coreutils on most Linux systems.

Quick Start

# Factor a single number
factor 360

# Factor multiple numbers
factor 84 97 1024

10 Practical Examples

# 1) Basic factorization
factor 84
# 2) Multiple numbers at once
factor 15 21 77
# 3) Read numbers from a file (one per line)
factor < numbers.txt
# 4) Factor a range (1..20)
seq 1 20 | factor
# 5) Get the distinct primes only
factor 360 | awk '{for(i=2;i<=NF;i++) seen[$i]=1} END{for(p in seen) print p}' | sort -n
# 6) Count prime factors (with multiplicity)
factor 360 | awk '{print $1, NF-1}'
# output: "360:" then count
# 7) Compute then factor (shell arithmetic)
factor $((2**16 - 1))
# 8) Find numbers in a range that are prime (exactly one factor)
seq 2 100 | factor | awk 'NF==2{print $1}' | tr -d ':'
# 9) Find numbers that are powers of two (only factor is 2)
seq 1 1024 | factor | awk '{ok=1; for(i=2;i<=NF;i++) if($i!=2) ok=0; if(ok && $1!="0:" && $1!="1:") print $1}' | tr -d ':'
# 10) Spot-check a big ID-like number
factor 4294967296

Notes & Gotchas

  • Input must be a non‑negative integer. Negative numbers and non-integers will error.
  • 0 and 1 are accepted but have no prime factors, so you’ll see just 0: or 1:.
  • Very large composites (especially the product of two large primes) can take a long time to factor.
  • If a token starts with -, it may be interpreted as an option. Use -- to end options (but the number still must be non‑negative).

Historical Context

Factoring utilities have been part of Unix-like systems for decades as handy tools for quick math checks, teaching, and scripting. GNU coreutils’ factor is the common implementation on Linux.

Modern Equivalent

For heavier-duty number theory, consider tools like sage, pari/gp, or Python libraries (e.g., sympy.factorint()) when you need advanced algorithms, better control, or huge integers.

Related Commands

  • bc — command-line calculator
  • python3 — scripting (e.g., SymPy for factorization)
  • seq — generate numeric ranges
  • primes / prime (if installed) — generate primes (package-dependent)