Advertisement
cwchen

Calculate factorial number with Bash

May 27th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.27 KB | None | 0 0
  1. #!/bin/bash
  2. # Bash is not a good tool for large number calculation.
  3. # Consider Python or Ruby
  4.  
  5. function fac {
  6.     if [ $1 -le 1 ]; then
  7.     echo 1
  8.     return
  9.     else
  10.     echo $(( $( fac $(( $1 - 1 )) ) * $1 ))
  11.     fi
  12. }
  13.  
  14. fac 1
  15. fac 10
  16. fac 20
  17. fac 50  # error
  18. fac 100 # error
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement