Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go dividing massive numbers (big.Int)

Tags:

go

I'm trying to divide two massive numbers (e.g. trying to divide 50! by 18!) and I have two big.Int variables set.

first.MulRange(1,50)

second.MulRange(1,18)

How can I divide the numbers (ideally with integer division)?

Thanks!

like image 621
elvinas Avatar asked Oct 07 '17 14:10

elvinas


Video Answer


1 Answers

How can I divide the numbers

By invoking Div() method of Int (in this case) data type. ("math/big" package)

first := new(big.Int).MulRange(1, 50)
second := new(big.Int).MulRange(1, 18)

fmt.Printf("First: %s \n", first.String())
fmt.Printf("Second: %s \n", second.String())
// division
dv := new(big.Int).Div(first, second)

fmt.Printf("Division result: %s \n", dv.String())

The result:

First: 30414093201713378043612608166064768844377641568960512000000000000
Second: 6402373705728000
Division result: 4750440164794325701367714688167999176704000000000
like image 158
Nick Krasnov Avatar answered Oct 13 '22 07:10

Nick Krasnov