Skip to main content
2 of 2
added 31 characters in body
Digital Trauma
  • 73.8k
  • 10
  • 117
  • 268

Bash + GNU utilities, 49

  • 9 bytes saved thanks to @Cowsquack
factor|sed '/:$/c-1 /: \w+$/c1 s%: %/% y/ /#/'|bc 

###Explanation

  • factor reads input numbers from STDIN, one per line and outputs in the format <input number>: <space-separated list of prime factors (ascending)>
  • sed processes this as follows:
    • /:$/c-1 The input numbers 0 and 1 have no prime factors and are replaced with -1
    • /: \w+$/c1 Numbers with one prime factor (themselves) are prime. Replace these with 1
    • s%: %/% Replace : with /. This builds an arithmetic expression to divide the (non-prime) input number by its smallest prime factor to give the largest factor
    • y/ /#/ Remove the list of other (unneeded) factors (by commenting out)
  • bc Arithmetically evaluate and display

Try it online!

Digital Trauma
  • 73.8k
  • 10
  • 117
  • 268