Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiply very big integer in .NET

Tags:

c#

.net

Assume that you have big integers like 1253999939979969998746 and 9999999999. Is there anyway to multiply these big integers in C#?

Note: I've tried System.Numerics.BigInteger class constructor compiler says that integer is too long.

What's your suggestion?

p.s warn me if this question out of topics.

like image 257
Mirjalal Avatar asked Dec 06 '22 17:12

Mirjalal


1 Answers

You need to use BigInteger.Parse.

BigInteger num = BigInteger.Parse("1253999939979969998746");

If you try to use an integer value like below

BigInteger num = 1253999939979969998746;

the right hand side needs to have a type, which by default will be an Int32 or a long. But since the number is too large to be an Int32 or a long, the conversion fails, hence the error you get.

As Tim Schmelter pointed out in a comment, to then do your multiplication, you can do the following:

BigInteger num = BigInteger.Multiply(BigInteger.Parse("1253999939979969998746"), 
                                     BigInteger.Parse("9999999999"));
like image 144
user1666620 Avatar answered Dec 17 '22 21:12

user1666620