NOS graf
NOS graf

Reputation: 29

How to print out large numbers in Bash

I tried out bc but if i try to find the factorial of number 50 it shows me 1 instead of 30414093201713378043612608166064768844377641568960512000000000000

 read T
while ((T--)); do
    read n
    factorial=1

    for ((i = 1; i <= $n; i++)); do
        factorial=$(( i * factorial ))
    done

    echo $factorial
done

Upvotes: 1

Views: 711

Answers (1)

Joan Esteban
Joan Esteban

Reputation: 1021

With bc works fine:

    echo "define f (x) {
                if (x <= 1) return (1);
               return (f(x-1) * x);
             }
   f(50) " | bc

Upvotes: 3

Related Questions