Sunday, April 17, 2016

Recursive Factorial in bc!

alias factorial='_() { var=${1}; echo -en "scale=25;\ndefine f (x) {if (x <= 1) return (1);\nreturn (f(x-1) * x);}\nf(${var})\nquit" | bc;};_' 
Logic found on the bc man page. Note the newlines, bc is a bit finicky about newlines when dealing with functions on one-liners otherwise.

No comments:

Post a Comment