Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

working with incredibly large numbers in .NET

I'm trying to work through the problems on projecteuler.net but I keep running into a couple of problems.

The first is a question of storing large quanities of elements in a List<t>. I keep getting OutOfMemoryException's when storing large quantities in the list.

Now I admit I might not be doing these things in the best way but, is there some way of defining how much memory the app can consume?

It usually crashes when I get abour 100,000,000 elements :S

Secondly, some of the questions require the addition of massive numbers. I use ulong data type where I think the number is going to get super big, but I still manage to wrap past the largest supported int and get into negative numbers.

Do you have any tips for working with incredibly large numbers?

like image 674
Greg B Avatar asked Nov 10 '08 20:11

Greg B


People also ask

How do you represent very large numbers?

Power notation is a method to express numbers that are very large or very small. For eg- 1000000000000 can be written as 1012 and 0.00000026 can be written as 2.6 × 10-7. Scientific notation is a similar method to express numbers in the form of nth power of 10. For eg; 1.67 × 107.

How do computers handle large numbers?

Representation You might use an array to hold all the digits of large numbers. A more efficient way would be to use an array of 32 bit unsigned integers and store "32 bit chunks" of the large number. You can think of these chunks as individual digits in a number system with 2^32 distinct digits or characters.

How do you find the highest number in C#?

CSharp Online Training int num1, num2, num3; // set the value of the three numbers num1 = 10; num2 = 20; num3 = 50; Now check the first number with the second number. If num1 > num2, then check num1 with num3. If num1 is greater than num3, that would mean the largest number is num1.

Where is large number used?

They are mostly used while calculating a country's population or while counting the money in a bank account. For example, 1 million and 1 billion are considered as large numbers.


1 Answers

Consider System.Numerics.BigInteger.

like image 97
Larsenal Avatar answered Sep 21 '22 18:09

Larsenal