Summation of large numbers

I am having some problems on the Project Euler website and are having problems. The question asks: "Generate the first ten digits of the sum from the next one hundred and fifty-digit numbers." I guess there is a mathematical way to solve this problem, but I'm just wondering how these numbers summarize? I store the number as a string and convert each digit to a long one, but the number is so large that the sum does not work.

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

+5
source share
5 answers

I'm just wondering how these big sums add up.

You can use an array:

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

:

  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

( )?

, (// )

, .

! ,

, , .

+5

. , 10 ^ 9. 182983198432847829347802092190

:

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

, arr [i] * (10 ^ 9i) = 0 , .

+1

I did in java, here I take the numbers N1 and N2, and I created an array of size 1000. Let's take an example How to solve this, N1 = 12, N2 = 1234. For N1 = 12, temp = N1% 10 = 2. Now add this digit with the digit N2 from right to left and save the result in an array, starting with i = 0, similarly for the resting digit N1. The array will save the result, but in reverse order. Take a look at this link. Check out 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;
    }
}
+1
source
#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;
}
0
source

This is 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;
}
0
source

All Articles