Factorial of certain numbers yield negative values

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Here is the code for my bash script to calculate factorials
read -p "Please enter the number " number
while ([ $number -gt 0 ])
do
factorial=1
for ((i=$number;i > 0;i--))
do
factorial=$((factorial * $i))
done
echo "The factorial of the " $number " is " $factorial
number=$((number - 1))
done
This prints out the factorial of all the numbers ranging from input:1
Everything looks right except for the factorial of a few. The output is given below. 
As you can see the factorials of certain numbers are negative values. I understand from the online forums that the bash script usually breaks when calculating factorial of bigger numbers but these negative values don't seem to be common as far as I could dig online. If someone could throw some light on the reason for it, it could greatly help me in my learning. Thank you!
linux bash shell-script
add a comment |Â
up vote
0
down vote
favorite
Here is the code for my bash script to calculate factorials
read -p "Please enter the number " number
while ([ $number -gt 0 ])
do
factorial=1
for ((i=$number;i > 0;i--))
do
factorial=$((factorial * $i))
done
echo "The factorial of the " $number " is " $factorial
number=$((number - 1))
done
This prints out the factorial of all the numbers ranging from input:1
Everything looks right except for the factorial of a few. The output is given below. 
As you can see the factorials of certain numbers are negative values. I understand from the online forums that the bash script usually breaks when calculating factorial of bigger numbers but these negative values don't seem to be common as far as I could dig online. If someone could throw some light on the reason for it, it could greatly help me in my learning. Thank you!
linux bash shell-script
3
Everything looks right, except that the results for values after 20 are wrong â and that should give you a hint as to whatâÂÂs going on here...
â Stephen Kitt
Dec 7 '17 at 14:55
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Here is the code for my bash script to calculate factorials
read -p "Please enter the number " number
while ([ $number -gt 0 ])
do
factorial=1
for ((i=$number;i > 0;i--))
do
factorial=$((factorial * $i))
done
echo "The factorial of the " $number " is " $factorial
number=$((number - 1))
done
This prints out the factorial of all the numbers ranging from input:1
Everything looks right except for the factorial of a few. The output is given below. 
As you can see the factorials of certain numbers are negative values. I understand from the online forums that the bash script usually breaks when calculating factorial of bigger numbers but these negative values don't seem to be common as far as I could dig online. If someone could throw some light on the reason for it, it could greatly help me in my learning. Thank you!
linux bash shell-script
Here is the code for my bash script to calculate factorials
read -p "Please enter the number " number
while ([ $number -gt 0 ])
do
factorial=1
for ((i=$number;i > 0;i--))
do
factorial=$((factorial * $i))
done
echo "The factorial of the " $number " is " $factorial
number=$((number - 1))
done
This prints out the factorial of all the numbers ranging from input:1
Everything looks right except for the factorial of a few. The output is given below. 
As you can see the factorials of certain numbers are negative values. I understand from the online forums that the bash script usually breaks when calculating factorial of bigger numbers but these negative values don't seem to be common as far as I could dig online. If someone could throw some light on the reason for it, it could greatly help me in my learning. Thank you!
linux bash shell-script
edited Dec 7 '17 at 14:56
asked Dec 7 '17 at 14:53
Ammu
274
274
3
Everything looks right, except that the results for values after 20 are wrong â and that should give you a hint as to whatâÂÂs going on here...
â Stephen Kitt
Dec 7 '17 at 14:55
add a comment |Â
3
Everything looks right, except that the results for values after 20 are wrong â and that should give you a hint as to whatâÂÂs going on here...
â Stephen Kitt
Dec 7 '17 at 14:55
3
3
Everything looks right, except that the results for values after 20 are wrong â and that should give you a hint as to whatâÂÂs going on here...
â Stephen Kitt
Dec 7 '17 at 14:55
Everything looks right, except that the results for values after 20 are wrong â and that should give you a hint as to whatâÂÂs going on here...
â Stephen Kitt
Dec 7 '17 at 14:55
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
6
down vote
accepted
bash arithmetic is done over signed 64-bits integers, so the maximum number is 9 223 372 036 854 775 807. If you go over it, you start from the opposite range, that is negative numbers. 20! is still below it, but not 21!
Okay. Got it! Thank you!
â Ammu
Dec 7 '17 at 15:06
And seebc,dc,python,gawk -M,perl -Mbignum... for arbitrary precision.
â Stéphane Chazelas
Dec 7 '17 at 15:19
unix.stackexchange.com/questions/40786/â¦
â Jeff Schaller
Dec 7 '17 at 15:30
@Ammu Remember to accept the correct answer!
â dreua
Dec 7 '17 at 16:29
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
bash arithmetic is done over signed 64-bits integers, so the maximum number is 9 223 372 036 854 775 807. If you go over it, you start from the opposite range, that is negative numbers. 20! is still below it, but not 21!
Okay. Got it! Thank you!
â Ammu
Dec 7 '17 at 15:06
And seebc,dc,python,gawk -M,perl -Mbignum... for arbitrary precision.
â Stéphane Chazelas
Dec 7 '17 at 15:19
unix.stackexchange.com/questions/40786/â¦
â Jeff Schaller
Dec 7 '17 at 15:30
@Ammu Remember to accept the correct answer!
â dreua
Dec 7 '17 at 16:29
add a comment |Â
up vote
6
down vote
accepted
bash arithmetic is done over signed 64-bits integers, so the maximum number is 9 223 372 036 854 775 807. If you go over it, you start from the opposite range, that is negative numbers. 20! is still below it, but not 21!
Okay. Got it! Thank you!
â Ammu
Dec 7 '17 at 15:06
And seebc,dc,python,gawk -M,perl -Mbignum... for arbitrary precision.
â Stéphane Chazelas
Dec 7 '17 at 15:19
unix.stackexchange.com/questions/40786/â¦
â Jeff Schaller
Dec 7 '17 at 15:30
@Ammu Remember to accept the correct answer!
â dreua
Dec 7 '17 at 16:29
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
bash arithmetic is done over signed 64-bits integers, so the maximum number is 9 223 372 036 854 775 807. If you go over it, you start from the opposite range, that is negative numbers. 20! is still below it, but not 21!
bash arithmetic is done over signed 64-bits integers, so the maximum number is 9 223 372 036 854 775 807. If you go over it, you start from the opposite range, that is negative numbers. 20! is still below it, but not 21!
answered Dec 7 '17 at 14:58
Patrick Mevzek
2,0381721
2,0381721
Okay. Got it! Thank you!
â Ammu
Dec 7 '17 at 15:06
And seebc,dc,python,gawk -M,perl -Mbignum... for arbitrary precision.
â Stéphane Chazelas
Dec 7 '17 at 15:19
unix.stackexchange.com/questions/40786/â¦
â Jeff Schaller
Dec 7 '17 at 15:30
@Ammu Remember to accept the correct answer!
â dreua
Dec 7 '17 at 16:29
add a comment |Â
Okay. Got it! Thank you!
â Ammu
Dec 7 '17 at 15:06
And seebc,dc,python,gawk -M,perl -Mbignum... for arbitrary precision.
â Stéphane Chazelas
Dec 7 '17 at 15:19
unix.stackexchange.com/questions/40786/â¦
â Jeff Schaller
Dec 7 '17 at 15:30
@Ammu Remember to accept the correct answer!
â dreua
Dec 7 '17 at 16:29
Okay. Got it! Thank you!
â Ammu
Dec 7 '17 at 15:06
Okay. Got it! Thank you!
â Ammu
Dec 7 '17 at 15:06
And see
bc, dc, python, gawk -M, perl -Mbignum... for arbitrary precision.â Stéphane Chazelas
Dec 7 '17 at 15:19
And see
bc, dc, python, gawk -M, perl -Mbignum... for arbitrary precision.â Stéphane Chazelas
Dec 7 '17 at 15:19
unix.stackexchange.com/questions/40786/â¦
â Jeff Schaller
Dec 7 '17 at 15:30
unix.stackexchange.com/questions/40786/â¦
â Jeff Schaller
Dec 7 '17 at 15:30
@Ammu Remember to accept the correct answer!
â dreua
Dec 7 '17 at 16:29
@Ammu Remember to accept the correct answer!
â dreua
Dec 7 '17 at 16:29
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409491%2ffactorial-of-certain-numbers-yield-negative-values%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
Everything looks right, except that the results for values after 20 are wrong â and that should give you a hint as to whatâÂÂs going on here...
â Stephen Kitt
Dec 7 '17 at 14:55