Como calcular um número fatorial em Bash puro - (Ex.: 5!) [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












Trying to translate:




What is a basic, elegant and fast way, to write factorial in Bash/in a
shell script, purely. (Code) Please make suggestions. (Code)




Tem outra alternativa mais básica/elegante/rápida de como fazer em poucas linhas de código uma maneira de calcular um número fatorial em BASH (Shell Script) puro.



Ex.: 5! = 5*4*3*2*1 = 120


Essa é minha sugestão:



num=5
echo $(($(eval echo $num..1 | sed 's/ /*/g')))






share|improve this question














closed as unclear what you're asking by Jeff Schaller, tachomi, Michael Homer, meuh, dr01 Feb 26 at 9:33


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    In English, per favore.
    – jasonwryan
    Feb 26 at 1:46










  • I can't read most of those words, but if you're looking for a factorial, try this or this or this or this
    – Jeff Schaller
    Feb 26 at 1:47










  • I will just mention that Stack Overflow in Spanish is a thing: es.stackoverflow.com
    – dhag
    Feb 26 at 3:24






  • 1




    @dhag This is Portuguese, not Spanish
    – Fox
    Feb 26 at 3:51






  • 1




    @Fox: Whoops. Thanks. Perhaps pt.stackoverflow.com would be an idea, then.
    – dhag
    Feb 26 at 5:31














up vote
-1
down vote

favorite












Trying to translate:




What is a basic, elegant and fast way, to write factorial in Bash/in a
shell script, purely. (Code) Please make suggestions. (Code)




Tem outra alternativa mais básica/elegante/rápida de como fazer em poucas linhas de código uma maneira de calcular um número fatorial em BASH (Shell Script) puro.



Ex.: 5! = 5*4*3*2*1 = 120


Essa é minha sugestão:



num=5
echo $(($(eval echo $num..1 | sed 's/ /*/g')))






share|improve this question














closed as unclear what you're asking by Jeff Schaller, tachomi, Michael Homer, meuh, dr01 Feb 26 at 9:33


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1




    In English, per favore.
    – jasonwryan
    Feb 26 at 1:46










  • I can't read most of those words, but if you're looking for a factorial, try this or this or this or this
    – Jeff Schaller
    Feb 26 at 1:47










  • I will just mention that Stack Overflow in Spanish is a thing: es.stackoverflow.com
    – dhag
    Feb 26 at 3:24






  • 1




    @dhag This is Portuguese, not Spanish
    – Fox
    Feb 26 at 3:51






  • 1




    @Fox: Whoops. Thanks. Perhaps pt.stackoverflow.com would be an idea, then.
    – dhag
    Feb 26 at 5:31












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Trying to translate:




What is a basic, elegant and fast way, to write factorial in Bash/in a
shell script, purely. (Code) Please make suggestions. (Code)




Tem outra alternativa mais básica/elegante/rápida de como fazer em poucas linhas de código uma maneira de calcular um número fatorial em BASH (Shell Script) puro.



Ex.: 5! = 5*4*3*2*1 = 120


Essa é minha sugestão:



num=5
echo $(($(eval echo $num..1 | sed 's/ /*/g')))






share|improve this question














Trying to translate:




What is a basic, elegant and fast way, to write factorial in Bash/in a
shell script, purely. (Code) Please make suggestions. (Code)




Tem outra alternativa mais básica/elegante/rápida de como fazer em poucas linhas de código uma maneira de calcular um número fatorial em BASH (Shell Script) puro.



Ex.: 5! = 5*4*3*2*1 = 120


Essa é minha sugestão:



num=5
echo $(($(eval echo $num..1 | sed 's/ /*/g')))








share|improve this question













share|improve this question




share|improve this question








edited Feb 26 at 3:09









user unknown

6,95412148




6,95412148










asked Feb 26 at 1:43









Helio Giroto

1




1




closed as unclear what you're asking by Jeff Schaller, tachomi, Michael Homer, meuh, dr01 Feb 26 at 9:33


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






closed as unclear what you're asking by Jeff Schaller, tachomi, Michael Homer, meuh, dr01 Feb 26 at 9:33


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









  • 1




    In English, per favore.
    – jasonwryan
    Feb 26 at 1:46










  • I can't read most of those words, but if you're looking for a factorial, try this or this or this or this
    – Jeff Schaller
    Feb 26 at 1:47










  • I will just mention that Stack Overflow in Spanish is a thing: es.stackoverflow.com
    – dhag
    Feb 26 at 3:24






  • 1




    @dhag This is Portuguese, not Spanish
    – Fox
    Feb 26 at 3:51






  • 1




    @Fox: Whoops. Thanks. Perhaps pt.stackoverflow.com would be an idea, then.
    – dhag
    Feb 26 at 5:31












  • 1




    In English, per favore.
    – jasonwryan
    Feb 26 at 1:46










  • I can't read most of those words, but if you're looking for a factorial, try this or this or this or this
    – Jeff Schaller
    Feb 26 at 1:47










  • I will just mention that Stack Overflow in Spanish is a thing: es.stackoverflow.com
    – dhag
    Feb 26 at 3:24






  • 1




    @dhag This is Portuguese, not Spanish
    – Fox
    Feb 26 at 3:51






  • 1




    @Fox: Whoops. Thanks. Perhaps pt.stackoverflow.com would be an idea, then.
    – dhag
    Feb 26 at 5:31







1




1




In English, per favore.
– jasonwryan
Feb 26 at 1:46




In English, per favore.
– jasonwryan
Feb 26 at 1:46












I can't read most of those words, but if you're looking for a factorial, try this or this or this or this
– Jeff Schaller
Feb 26 at 1:47




I can't read most of those words, but if you're looking for a factorial, try this or this or this or this
– Jeff Schaller
Feb 26 at 1:47












I will just mention that Stack Overflow in Spanish is a thing: es.stackoverflow.com
– dhag
Feb 26 at 3:24




I will just mention that Stack Overflow in Spanish is a thing: es.stackoverflow.com
– dhag
Feb 26 at 3:24




1




1




@dhag This is Portuguese, not Spanish
– Fox
Feb 26 at 3:51




@dhag This is Portuguese, not Spanish
– Fox
Feb 26 at 3:51




1




1




@Fox: Whoops. Thanks. Perhaps pt.stackoverflow.com would be an idea, then.
– dhag
Feb 26 at 5:31




@Fox: Whoops. Thanks. Perhaps pt.stackoverflow.com would be an idea, then.
– dhag
Feb 26 at 5:31










1 Answer
1






active

oldest

votes

















up vote
2
down vote













This works without eval:



prod=$(echo 1..5* 1); echo $((prod))


Here is a bash function, which takes an parameter, what isn't possible in the above solution:



fak () 

echo $(($(seq -s'*' 1 $1)))



As cas pointed out in the comments, the Shell is limited to 64 bits, for bigger results, bc is worth a try, but that isn't puro, is it? Well - that argument count's for seq too; it's an external program.



echo 1..30* 1 | bc





share|improve this answer






















  • +1 for the 1..5* 1, but it's probably better to pipe into bc rather than use bash's built-in arithmetic. bc can work with integers of any arbitrary precision, while shell has a maxint value (determined mostly by cpu architecture it was compiled on - e.g. 64 bit int on a 64 bit cpu). For example, prod=$(echo 1..30* 1); echo $((prod)) prints -8764578968847253504 (clearly wrong), while echo 1..30* 1 | bc prints 265252859812191058636308480000000 (presumably correct).
    – cas
    Feb 26 at 2:11










  • Yes, bc reaches far wider, while not arbitrary wide - test echo "9^9^9 | bc". And afaik, bash calculated with 64 bits when I still used a 32bit PC, but I'm not 100% sure about that. I don't speak Brazilian/Portuguese, but "em BASH (Shell Script) puro" sounds much like "in purely Bash" doesn't it? (I updated the translation). And the example 5! didn't look that ambitious, too. :)
    – user unknown
    Feb 26 at 3:08











  • the bc docs & man page say "arbitrary" so that's what i copied :)
    – cas
    Feb 26 at 3:13










  • BTW, piping into bc works for at least up to 99999!. That took about 5 minutes to run on my machine, which was boring enough to discourage me from trying larger numbers.
    – cas
    Feb 26 at 3:22











  • afaik echo "8^8^8 | bc" fails too, but "7^7^7 | bc" should work. :)
    – user unknown
    Feb 26 at 3:28

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote













This works without eval:



prod=$(echo 1..5* 1); echo $((prod))


Here is a bash function, which takes an parameter, what isn't possible in the above solution:



fak () 

echo $(($(seq -s'*' 1 $1)))



As cas pointed out in the comments, the Shell is limited to 64 bits, for bigger results, bc is worth a try, but that isn't puro, is it? Well - that argument count's for seq too; it's an external program.



echo 1..30* 1 | bc





share|improve this answer






















  • +1 for the 1..5* 1, but it's probably better to pipe into bc rather than use bash's built-in arithmetic. bc can work with integers of any arbitrary precision, while shell has a maxint value (determined mostly by cpu architecture it was compiled on - e.g. 64 bit int on a 64 bit cpu). For example, prod=$(echo 1..30* 1); echo $((prod)) prints -8764578968847253504 (clearly wrong), while echo 1..30* 1 | bc prints 265252859812191058636308480000000 (presumably correct).
    – cas
    Feb 26 at 2:11










  • Yes, bc reaches far wider, while not arbitrary wide - test echo "9^9^9 | bc". And afaik, bash calculated with 64 bits when I still used a 32bit PC, but I'm not 100% sure about that. I don't speak Brazilian/Portuguese, but "em BASH (Shell Script) puro" sounds much like "in purely Bash" doesn't it? (I updated the translation). And the example 5! didn't look that ambitious, too. :)
    – user unknown
    Feb 26 at 3:08











  • the bc docs & man page say "arbitrary" so that's what i copied :)
    – cas
    Feb 26 at 3:13










  • BTW, piping into bc works for at least up to 99999!. That took about 5 minutes to run on my machine, which was boring enough to discourage me from trying larger numbers.
    – cas
    Feb 26 at 3:22











  • afaik echo "8^8^8 | bc" fails too, but "7^7^7 | bc" should work. :)
    – user unknown
    Feb 26 at 3:28














up vote
2
down vote













This works without eval:



prod=$(echo 1..5* 1); echo $((prod))


Here is a bash function, which takes an parameter, what isn't possible in the above solution:



fak () 

echo $(($(seq -s'*' 1 $1)))



As cas pointed out in the comments, the Shell is limited to 64 bits, for bigger results, bc is worth a try, but that isn't puro, is it? Well - that argument count's for seq too; it's an external program.



echo 1..30* 1 | bc





share|improve this answer






















  • +1 for the 1..5* 1, but it's probably better to pipe into bc rather than use bash's built-in arithmetic. bc can work with integers of any arbitrary precision, while shell has a maxint value (determined mostly by cpu architecture it was compiled on - e.g. 64 bit int on a 64 bit cpu). For example, prod=$(echo 1..30* 1); echo $((prod)) prints -8764578968847253504 (clearly wrong), while echo 1..30* 1 | bc prints 265252859812191058636308480000000 (presumably correct).
    – cas
    Feb 26 at 2:11










  • Yes, bc reaches far wider, while not arbitrary wide - test echo "9^9^9 | bc". And afaik, bash calculated with 64 bits when I still used a 32bit PC, but I'm not 100% sure about that. I don't speak Brazilian/Portuguese, but "em BASH (Shell Script) puro" sounds much like "in purely Bash" doesn't it? (I updated the translation). And the example 5! didn't look that ambitious, too. :)
    – user unknown
    Feb 26 at 3:08











  • the bc docs & man page say "arbitrary" so that's what i copied :)
    – cas
    Feb 26 at 3:13










  • BTW, piping into bc works for at least up to 99999!. That took about 5 minutes to run on my machine, which was boring enough to discourage me from trying larger numbers.
    – cas
    Feb 26 at 3:22











  • afaik echo "8^8^8 | bc" fails too, but "7^7^7 | bc" should work. :)
    – user unknown
    Feb 26 at 3:28












up vote
2
down vote










up vote
2
down vote









This works without eval:



prod=$(echo 1..5* 1); echo $((prod))


Here is a bash function, which takes an parameter, what isn't possible in the above solution:



fak () 

echo $(($(seq -s'*' 1 $1)))



As cas pointed out in the comments, the Shell is limited to 64 bits, for bigger results, bc is worth a try, but that isn't puro, is it? Well - that argument count's for seq too; it's an external program.



echo 1..30* 1 | bc





share|improve this answer














This works without eval:



prod=$(echo 1..5* 1); echo $((prod))


Here is a bash function, which takes an parameter, what isn't possible in the above solution:



fak () 

echo $(($(seq -s'*' 1 $1)))



As cas pointed out in the comments, the Shell is limited to 64 bits, for bigger results, bc is worth a try, but that isn't puro, is it? Well - that argument count's for seq too; it's an external program.



echo 1..30* 1 | bc






share|improve this answer














share|improve this answer



share|improve this answer








edited Feb 26 at 3:16

























answered Feb 26 at 1:52









user unknown

6,95412148




6,95412148











  • +1 for the 1..5* 1, but it's probably better to pipe into bc rather than use bash's built-in arithmetic. bc can work with integers of any arbitrary precision, while shell has a maxint value (determined mostly by cpu architecture it was compiled on - e.g. 64 bit int on a 64 bit cpu). For example, prod=$(echo 1..30* 1); echo $((prod)) prints -8764578968847253504 (clearly wrong), while echo 1..30* 1 | bc prints 265252859812191058636308480000000 (presumably correct).
    – cas
    Feb 26 at 2:11










  • Yes, bc reaches far wider, while not arbitrary wide - test echo "9^9^9 | bc". And afaik, bash calculated with 64 bits when I still used a 32bit PC, but I'm not 100% sure about that. I don't speak Brazilian/Portuguese, but "em BASH (Shell Script) puro" sounds much like "in purely Bash" doesn't it? (I updated the translation). And the example 5! didn't look that ambitious, too. :)
    – user unknown
    Feb 26 at 3:08











  • the bc docs & man page say "arbitrary" so that's what i copied :)
    – cas
    Feb 26 at 3:13










  • BTW, piping into bc works for at least up to 99999!. That took about 5 minutes to run on my machine, which was boring enough to discourage me from trying larger numbers.
    – cas
    Feb 26 at 3:22











  • afaik echo "8^8^8 | bc" fails too, but "7^7^7 | bc" should work. :)
    – user unknown
    Feb 26 at 3:28
















  • +1 for the 1..5* 1, but it's probably better to pipe into bc rather than use bash's built-in arithmetic. bc can work with integers of any arbitrary precision, while shell has a maxint value (determined mostly by cpu architecture it was compiled on - e.g. 64 bit int on a 64 bit cpu). For example, prod=$(echo 1..30* 1); echo $((prod)) prints -8764578968847253504 (clearly wrong), while echo 1..30* 1 | bc prints 265252859812191058636308480000000 (presumably correct).
    – cas
    Feb 26 at 2:11










  • Yes, bc reaches far wider, while not arbitrary wide - test echo "9^9^9 | bc". And afaik, bash calculated with 64 bits when I still used a 32bit PC, but I'm not 100% sure about that. I don't speak Brazilian/Portuguese, but "em BASH (Shell Script) puro" sounds much like "in purely Bash" doesn't it? (I updated the translation). And the example 5! didn't look that ambitious, too. :)
    – user unknown
    Feb 26 at 3:08











  • the bc docs & man page say "arbitrary" so that's what i copied :)
    – cas
    Feb 26 at 3:13










  • BTW, piping into bc works for at least up to 99999!. That took about 5 minutes to run on my machine, which was boring enough to discourage me from trying larger numbers.
    – cas
    Feb 26 at 3:22











  • afaik echo "8^8^8 | bc" fails too, but "7^7^7 | bc" should work. :)
    – user unknown
    Feb 26 at 3:28















+1 for the 1..5* 1, but it's probably better to pipe into bc rather than use bash's built-in arithmetic. bc can work with integers of any arbitrary precision, while shell has a maxint value (determined mostly by cpu architecture it was compiled on - e.g. 64 bit int on a 64 bit cpu). For example, prod=$(echo 1..30* 1); echo $((prod)) prints -8764578968847253504 (clearly wrong), while echo 1..30* 1 | bc prints 265252859812191058636308480000000 (presumably correct).
– cas
Feb 26 at 2:11




+1 for the 1..5* 1, but it's probably better to pipe into bc rather than use bash's built-in arithmetic. bc can work with integers of any arbitrary precision, while shell has a maxint value (determined mostly by cpu architecture it was compiled on - e.g. 64 bit int on a 64 bit cpu). For example, prod=$(echo 1..30* 1); echo $((prod)) prints -8764578968847253504 (clearly wrong), while echo 1..30* 1 | bc prints 265252859812191058636308480000000 (presumably correct).
– cas
Feb 26 at 2:11












Yes, bc reaches far wider, while not arbitrary wide - test echo "9^9^9 | bc". And afaik, bash calculated with 64 bits when I still used a 32bit PC, but I'm not 100% sure about that. I don't speak Brazilian/Portuguese, but "em BASH (Shell Script) puro" sounds much like "in purely Bash" doesn't it? (I updated the translation). And the example 5! didn't look that ambitious, too. :)
– user unknown
Feb 26 at 3:08





Yes, bc reaches far wider, while not arbitrary wide - test echo "9^9^9 | bc". And afaik, bash calculated with 64 bits when I still used a 32bit PC, but I'm not 100% sure about that. I don't speak Brazilian/Portuguese, but "em BASH (Shell Script) puro" sounds much like "in purely Bash" doesn't it? (I updated the translation). And the example 5! didn't look that ambitious, too. :)
– user unknown
Feb 26 at 3:08













the bc docs & man page say "arbitrary" so that's what i copied :)
– cas
Feb 26 at 3:13




the bc docs & man page say "arbitrary" so that's what i copied :)
– cas
Feb 26 at 3:13












BTW, piping into bc works for at least up to 99999!. That took about 5 minutes to run on my machine, which was boring enough to discourage me from trying larger numbers.
– cas
Feb 26 at 3:22





BTW, piping into bc works for at least up to 99999!. That took about 5 minutes to run on my machine, which was boring enough to discourage me from trying larger numbers.
– cas
Feb 26 at 3:22













afaik echo "8^8^8 | bc" fails too, but "7^7^7 | bc" should work. :)
– user unknown
Feb 26 at 3:28




afaik echo "8^8^8 | bc" fails too, but "7^7^7 | bc" should work. :)
– user unknown
Feb 26 at 3:28


Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?