# Sum of first N natural numbers when N is extremely large

Given a positive integer n, the task is to find the sum of the first n natural numbers given that n is very large (1 ≤ n ≤ 1020000).

Examples:

Input: n = 4
Output: 10
1 + 2 + 3 + 4 = 10

Input: n = 12345678910
Output: 76207893880582233505

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Sum of first n natural numbers is (n * (n + 1)) / 2 but given that n can be extremely large (1 ≤ n ≤ 1020000). Now its obvious that we can only store the sum in a string. A simple solution is to run a loop till n and calculate the sum by the method of addition of two strings and iteratively add all numbers one by one but time complexity of this solution will be very large.
We can optimise this solution using BigInteger class in java. BigInteger class gives pre-defined methods for Mathematical operations which can be used to solve (n * (n + 1)) / 2 to calculate the required result.

• Take a string for holding the value of extremely large input.
• Transform this string to a BigInteger.
• Calculate (n * (n + 1)) / 2 using BigInteger class’s pre-defined methods.
• Print the calculated sum in the end.

Below is the implementation of the above approach:

## Java

 `// Java program to find the sum of the first n ` `//  natural numbers  when n is very large ` `import` `java.math.BigInteger; ` `class` `GeeksForGeeks { ` ` `  `    ``// Function to return the sum of first  ` `    ``// n natural numbers ` `    ``static` `BigInteger sum(String n) ` `    ``{ ` `        ``// b1 = 1 ` `        ``BigInteger b1 = BigInteger.ONE; ` ` `  `        ``// b2 = 2 ` `        ``BigInteger b2 = ``new` `BigInteger(``"2"``); ` ` `  `        ``// Converting n to BigInteger ` `        ``BigInteger bigInt = ``new` `BigInteger(n); ` ` `  `        ``// Calculating (n * (n + 1)) / 2 ` `        ``BigInteger result = ` `         ``(bigInt.multiply(bigInt.add(b1))).divide(b2); ` `        ``return` `result; ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `                   ``throws` `java.lang.Exception ` `    ``{ ` `        ``String n = ``"12345678910"``; ` `        ``System.out.println(sum(n)); ` `    ``} ` `} `

## Python3

 `# Python3 program to find the sum  ` `# of first n natural numbers when  ` `# n is very large  ` ` `  `# Function to return the sum of  ` `# first n natural numbers  ` `def` `Sum``(n):  ` `     `  `    ``result ``=` `(n ``*` `(n ``+` `1``)) ``/``/` `2` `     `  `    ``return` `result  ` ` `  `# Driver Code     ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``n ``=` `"12345678910"` `    ``print``(``Sum``(``int``(n)))  ` ` `  `# This code is contributed  ` `# by Rituraj_Jain `

## PHP

 ` `

Output:

```76207893880582233505
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : rituraj_jain, Akanksha_Rai

Article Tags :
Practice Tags :

3

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.