Skip to content

Instantly share code, notes, and snippets.

@travishen
Last active September 7, 2021 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travishen/d2b983d23e8ec6395185762621363e98 to your computer and use it in GitHub Desktop.
Save travishen/d2b983d23e8ec6395185762621363e98 to your computer and use it in GitHub Desktop.
Factorize integer using awk
# awk -f factorize.awk
{
n = int($1)
m = n = (n>=2) ? n: 2
factors = ""
for (k = 2; (m > 1) && (k^2 <= n); )
{
if (int(m % k) != 0)
{
k++
continue
}
m /= k
factors = (factors == "") ? ("" k) : (factors " * " k)
}
if ((1 < m) && (m < n))
factors = factors " * " m
print n, (factors == "") ? "is prime" : ("= " factors)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment