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

Bash + GNU utilities, 5849

  • 9 bytes saved thanks to @Cowsquack
factor|sed -r 's'/.:$/c-1/ s/.*: \w+$/1/c1 s%: %/% sy/ .*/#/'|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:
    • s/.:$/c-1/ The input numbers 0 and 1 have no prime factors and are replaced with -1
    • s/.*: \w+$/1/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
    • sy/ .*/#/ Remove the list of other (unneeded) factors (by commenting out)
  • bc Arithmetically evaluate and display

Try it online!Try it online!

Bash + GNU utilities, 58

factor|sed -r 's/.:$/-1/ s/.*: \w+$/1/ s%: %/% s/ .*//'|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:
    • s/.:$/-1/ The input numbers 0 and 1 have no prime factors and are replaced with -1
    • s/.*: \w+$/1/ 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
    • s/ .*// Remove the list of other (unneeded) factors
  • bc Arithmetically evaluate and display

Try it online!

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!

Source Link
Digital Trauma
  • 73.8k
  • 10
  • 117
  • 268

Bash + GNU utilities, 58

factor|sed -r 's/.:$/-1/ s/.*: \w+$/1/ s%: %/% s/ .*//'|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:
    • s/.:$/-1/ The input numbers 0 and 1 have no prime factors and are replaced with -1
    • s/.*: \w+$/1/ 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
    • s/ .*// Remove the list of other (unneeded) factors
  • bc Arithmetically evaluate and display

Try it online!