2

I am trying to write a program that sums very big numbers (I am trying to solve a problem on projecteuler.net), so I cannot parse them into number types. So I was wondering if it is possible to sum such numbers using only strings or something like that?

4
  • No, no you can not. Commented Aug 2, 2018 at 20:39
  • Not sure if this is what you need, see stackoverflow.com/questions/10256351/sum-a-list-of-bigintegers Commented Aug 2, 2018 at 20:41
  • Have a look at BigInteger: msdn.microsoft.com/en-us/library/… Commented Aug 2, 2018 at 20:42
  • 3
    Numbers are just a series of symbols, more precisely digits. An addition can be performed just by stepping from right to left and performing single-digit additions and taking the 'carry' value to the next step. You can repeat this as many times you like. As you did on paper in elementary school. It won't be performant, but it works if this is what you want. Commented Aug 2, 2018 at 20:44

3 Answers 3

8

Try using BigInteger instead. It effictively lets you use integers of arbitrary size.

Sign up to request clarification or add additional context in comments.

2 Comments

Absolutely, +1. The OP is only adding, but as a note for other readers, the implementations of math functions like multiplication are not optimized for very large numbers. If you need fast huge-integer math, have a look at something like GNU MP (for which C# bindings are available or you can make your own with P/Invoke).
Thanks so much, BigInteger did exacly what I wanted!
2

BigInteger Represents an arbitrarily large signed integer. microsoft documentation.

class Program
{
    static void Main(string[] args)
    {

        string inputString = Console.ReadLine();

        string[] linePart = inputString.Split(' ', ',', '\n');  // split as , and " " and new line
       /* Console.WriteLine(linePart[1]);*/
        string num0 = linePart[0];
        
        string num1 = linePart[1];
        int val0 = num0.Length;
        int iv0 = val0;
        int val1 = num1.Length;
        int iv1 = val1;
      /*  Console.WriteLine(val0);
        Console.WriteLine(val1)*/;
        int arraySize;
        if (val0 > val1)
        {
            arraySize = val0;

        }
        else { arraySize = val1; }
        int[] arr0 = new int[arraySize];
        int[] arr1 = new int[arraySize];
        for (int i =0; i <iv0; i++)
        {
            arr0[i] = num0[val0-1]-48;
            val0--;
        }
        for (int i = 0; i < iv1; i++)
        {
            arr1[i] = num1[val1-1]-48;
            val1--;
        }
    /*    for (int i = 0; i < arraySize; i++)
        {
            Console.Write(arr0[i]);
            Console.Write(" ");
            Console.Write(arr1[i]);
            Console.WriteLine();
        }*/
        int tamp=0;
        int rem=0;
        int[] ans = new int[arraySize+1];
        int ansloop = arraySize;
        for(int i = 0; i <= arraySize; i++)
        {
            if (i != arraySize)
            {
                tamp = arr0[i] + arr1[i];
                tamp = tamp + rem;
                if (tamp >= 10)
                {
                    tamp = tamp % 10;
                    rem = 1;
                }
                else { rem = 0; }
                ans[ansloop] = tamp;
                ansloop--;
            }
            else {
                ans[ansloop] = rem;
            }
            
        }
        for (int i = 0; i <= arraySize; i++)
        {
            Console.Write(ans[i]+" ");
         
        }


    }
}

}

1 Comment

Welcome to StackOverflow. While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.
1

kindly use BigInteger found in System.Numerics and use `

  • Add(BigInteger, BigInteger)

` please follow the below link for better understanding.

add two bigintegers

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.