5

I have being doing some problems on the Project Euler website and have come across a problem. The Question asks,"Work out the first ten digits of the sum of the following one-hundred 50-digit numbers." I am guessing there is some mathematical way to solve this but I was just wondering how numbers this big are summed? I store the number as a string and convert each digit to a long but the number is so large that the sum does not work.

Is there a way to hold very large numbers as a variable (that is not a string)? I do not want the code to the problem as I want to solve that for myself.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
Jake Runzer
  • 957
  • 3
  • 17
  • 35
  • 2
    have you tried an array? – Thomas Jones May 08 '12 at 05:07
  • i used a `std::deque ` in my implementation of large integers – calccrypto May 08 '12 at 05:10
  • I didn't try one of those but would it work to sum the number? It could hold them but I need to sum them. – Jake Runzer May 08 '12 at 05:14
  • There are several C++ libraries for big integer arithmetic, like [here](https://mattmccutchen.net/bigint/) and [here](http://sourceforge.net/projects/cpp-bigint/). – smocking May 08 '12 at 05:16
  • I have yet to implement the library but from what the library is supposed to do, it should work. How much memory do these variables use? In this program it does not matter, I am just curious. – Jake Runzer May 08 '12 at 05:27
  • 5
    My guess is think about how you do sums in the first years of school. Lining the numbers up and adding one digit at a time. – dutt May 08 '12 at 05:29

5 Answers5

4

I was just wondering how numbers this big are summed?

You can use an array:

long LargeNumber[5] = { < first_10_digits>, < next_10_digits>....< last_10_digits> };

Now you can calculate the sum of 2 large numbers:

  long tempSum = 0;
  int carry = 0;
  long sum[5] = {0,0,0,0,0};

  for(int i = 4; i >= 0; i--)
  {
    tempSum = largeNum1[i] + largeNum2[i] + carry; //sum of 10 digits

    if( i == 0)
      sum[i] = tempSum; //No carry in case of most significant digit
    else
      sum[i] = tempSum % 1000000000; //Extra digits to be 'carried over'

    carry = tempSum/1000000000;
  }

  for( int i = 0; i < 5; i++ )
    cout<<setw(10)<<setfill('0')<<sum[i]<<"\n"; //Pad with '0' on the left if needed

Is there a way to hold very large numbers as a variable (that is not a string)?

There's no primitive for this, you can use any data structure (arrays/queues/linkedlist) and handle it suitably

I am guessing there is some mathematical way to solve this

Of course! But,

I do not want the code to the problem as I want to solve that for myself.

André Fratelli
  • 5,920
  • 7
  • 46
  • 87
vidit
  • 957
  • 1
  • 8
  • 23
1

You may store the digits in an array. To save space and increase performance in the operations, store the digits of the number in base 10^9. so a number 182983198432847829347802092190 will be represented as the following in the array

arr[0]=2092190 arr[1]=78293478 arr[2]=19843284 arr[3]=182983

just for the sake of clarity, the number is represented as summation of arr[i]*(10^9i) now start with i=0 and start adding the numbers the way you learnt as a kid.

nims
  • 3,751
  • 1
  • 23
  • 27
1

I have done in java, Here I am taking to numbers N1 and N2, And I have create an array of size 1000. Lets take an example How to solve this, N1=12, N2=1234. For N1=12, temp=N1%10=2, Now add this digit with digit N2 from right to Left and store the result into array starting from i=0, similarly for rest digit of N1. The array will store the result but in reverse order. Have a looking on this link. Please check this link http://ideone.com/V5knEd

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception  {
        Scanner scan=new Scanner(System.in);
        int A=scan.nextInt();
        int B=scan.nextInt();
        int [] array=new int[1000];
        Arrays.fill(array,0);
        int size=add(A,B,array);
        for(int i=size-1;i>=0;i--){
            System.out.print(array[i]);
        }
    }
    public static int add(int A, int B, int [] array){
        int carry=0;
        int i=0;
        while(A>0 || B>0){
            int sum=A%10+B%10+carry+array[i];
            array[i]=sum%10;
            carry=sum/10;
            A=A/10;
            B=B/10;
            i++;
        }
        while(carry>0){
            array[i]=array[i]+carry%10;
            carry=carry/10;
            i++;
        }
        return i;
    }
}
Neelabh Singh
  • 2,600
  • 12
  • 51
  • 88
0
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;

struct grid{
    int num[50];
};

int main()
{
    struct grid obj[100];
    char ch;
    ifstream myfile ("numbers.txt");
    if (myfile.is_open())
    {
        for(int r=0; r<100; r++)
        {
            for(int c=0; c<50; c++)
            {
                myfile >> ch;
                obj[r].num[c] = ch - '0';
            }
        }
        myfile.close();
        int result[50],temp_sum = 0;
        for (int c = 49; c>=0; c--)
        {
            for (int r=0; r<100; r++)
            {
                temp_sum += obj[r].num[c];
            }
            result[c] = temp_sum%10;
            temp_sum = temp_sum/10;
        }
        string temp;
        ostringstream convert;
        convert << temp_sum;
        temp = convert.str();
        cout << temp_sum;
        for(unsigned int count = 0; count < (10 - temp.length()); count++)
        {
            cout << result[count];
        }
        cout << endl;
    }
    return 0;
}
Mojo Jojo
  • 356
  • 3
  • 14
0

This the best way for your time and memory size :D

#include <iostream >
#include <climits >

using namespace std;

int main()
{
    unsigned long long  z;

    cin >>z;

    z=z*(z+1)/2;

    C out << z;

    return 0;
}
AJPerez
  • 3,435
  • 10
  • 61
  • 91
  • this also working if u want to sum the range between 1 to the input from the user til 10^13 if u want more than 10^13 ....u can change unsigned long long to long double :D – Ahmed Shoman Oct 17 '14 at 06:07
  • Is an unsigned long long going to store 50 digit numbers? Did you check that? – Mishax Oct 17 '14 at 06:14