Factorial of certain numbers yield negative values

The name of the pictureThe name of the pictureThe name of the pictureClash 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.
Output of the code to calculate factorial
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!







share|improve this question


















  • 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














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.
Output of the code to calculate factorial
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!







share|improve this question


















  • 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












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.
Output of the code to calculate factorial
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!







share|improve this question














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.
Output of the code to calculate factorial
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!









share|improve this question













share|improve this question




share|improve this question








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












  • 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










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!






share|improve this answer




















  • 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










  • 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










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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






























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!






share|improve this answer




















  • 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










  • 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














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!






share|improve this answer




















  • 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










  • 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












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!






share|improve this answer












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!







share|improve this answer












share|improve this answer



share|improve this answer










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 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










  • @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










  • 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










  • @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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)