Skip to content

kszongic/prime-factors-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@kszongic/prime-factors-cli

npm version npm downloads license node zero dependencies platform

Factorize integers into their prime factors from the command line. Zero dependencies.

$ prime-factors 360
2 x 2 x 2 x 3 x 3 x 5

$ prime-factors 360 --exponent
2^3 × 3^2 × 5

Why?

Need to quickly check prime factors while coding, debugging crypto, or doing math homework? Instead of opening a browser and searching "prime factorization of 1234567", just:

npx @kszongic/prime-factors-cli 1234567
# 127 x 9721
  • Instant — no website, no Python, no calculator app
  • 📦 Zero dependencies — installs in under a second
  • 🖥️ Cross-platform — Windows, macOS, Linux
  • 🔗 Pipe-friendly — JSON output for scripting

Install

npm i -g @kszongic/prime-factors-cli

Or run directly without installing:

npx @kszongic/prime-factors-cli 42

Usage

Basic factorization

prime-factors 60
# 2 x 2 x 3 x 5

Multiple numbers

prime-factors 100 72 360
# 100 = 2 x 2 x 5 x 5
# 72 = 2 x 2 x 2 x 3 x 3
# 360 = 2 x 2 x 2 x 3 x 3 x 5

Exponent notation

prime-factors 360 --exponent
# 2^3 x 3^2 x 5

prime-factors 1000000 --exponent
# 2^6 x 5^6

JSON output

prime-factors 60 --json
# {"input":60,"factors":[2,2,3,5]}

prime-factors 60 --json --exponent
# {"input":60,"factors":{"2":2,"3":1,"5":1}}

Pipe into other tools

# Find the largest prime factor
prime-factors 2310 --json | jq '.factors[-1]'
# 11

# Batch factorize from a file
cat numbers.txt | xargs prime-factors --json

Options

Flag Description
<number> [number...] One or more positive integers to factorize
--exponent Show exponent notation (e.g. 2^3 x 3)
--json Output as JSON (great for piping)
-h, --help Show help

Recipes

🔑 RSA key analysis

# Quick check if a number is prime (single factor = prime)
prime-factors 997
# 997
# Just the number itself — it's prime!

📊 GCD / LCM helper

# Factorize two numbers to find GCD manually
prime-factors 120 84 --exponent
# 120 = 2^3 x 3 x 5
# 84 = 2^2 x 3 x 7
# GCD = 2^2 x 3 = 12

🧪 Project Euler / competitive programming

# Quick factorization during problem-solving
prime-factors 600851475143

📝 npm scripts

{
  "scripts": {
    "factor": "prime-factors"
  }
}

How It Works

Uses trial division — the simplest and most intuitive factorization algorithm:

  1. Start with the smallest prime (2)
  2. While the number is divisible, divide and record the factor
  3. Move to the next candidate
  4. Stop when the candidate exceeds √n

This is efficient for numbers up to ~10^15. For larger numbers, you'd need more advanced algorithms (Pollard's rho, quadratic sieve), but for CLI use this covers virtually all practical cases.

Use Cases

  • Math homework — quick factorization without leaving the terminal
  • Cryptography — check if a number is prime, analyze key components
  • Competitive programming — fast factor lookup during contests
  • Teaching — demonstrate prime factorization interactively
  • Number theory exploration — find patterns, verify conjectures

Comparison

Tool Zero deps Cross-platform JSON output Exponent notation Install
prime-factors-cli ✅ Win/Mac/Linux npx @kszongic/prime-factors-cli
factor (coreutils) N/A ❌ Unix only Built-in (Linux)
Python one-liner N/A Manual Manual Requires Python
Wolfram Alpha N/A ✅ (browser) API only Browser / API key
Online calculators N/A ✅ (browser) Varies None

Related

License

MIT © 2026 kszongic

Releases

No releases published

Packages

 
 
 

Contributors