Given two very large numbers L and R where L ? R, the task is to compute the sum of all the natural numbers from L to R. The sum could be large so print the sum % 1000000007.
Examples:
Input: L = “8894” R = “98592”
Output: 820693329
Input: L = “88949273204” R = “98429729474298592”
Output: 252666158
Approach:
- Let sum(N) is a function that returns the sum of first N natural numbers.
- The sum of the first N natural numbers is sum(N) = (N * (N + 1)) / 2.
- The sum of the numbers in the range between L to R will be RangeSum = sum(R) – sum(L – 1)
- The answer is calculated with the modulo 109 + 7, So,
mod = 109 + 7
RangeSum = (sum(R) – sum(L-1) + mod)%mod;
This can be also written as RangeSum = (sum(R)%mod – sum(L-1)%mod + mod)%mod;
Now, sum(R) % mod can be written as ((R * (R + 1)) / 2) % mod
Or ((R % mod) * ((R + 1) % mod) * invmod(2)) % mod
Since R is large, the modulo of R can be calculated as described here.
The value of inversemod(2) = 500000004 which can be calculated using Fermat’s little theorem.
Similarly, sum(L – 1) % mod can also be calculated.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
const long long inv2 = 500000004;
long long int modulo(string num)
{
long long int res = 0;
for ( long long int i = 0;
i < num.length();
i++)
res = (res * 10
+ ( long long int )num[i]
- '0' )
% mod;
return res;
}
long long int findSum(string L,
string R)
{
long long int a, b, l, r, ret;
a = modulo(L);
b = modulo(R);
l = ((a * (a - 1))
% mod * inv2)
% mod;
r = ((b * (b + 1))
% mod * inv2)
% mod;
ret = (r % mod - l % mod);
if (ret < 0)
ret = ret + mod;
else
ret = ret % mod;
return ret;
}
int main()
{
string L = "88949273204" ;
string R = "98429729474298592" ;
cout << findSum(L, R) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static long mod = 1000000007 ;
static long inv2 = 500000004 ;
static long modulo(String num)
{
long res = 0 ;
for ( int i = 0 ;
i < num.length(); i++)
res = (res * 10 +
( long )num.charAt(i) - '0' ) % mod;
return res;
}
static long findSum(String L, String R)
{
long a, b, l, r, ret;
a = modulo(L);
b = modulo(R);
l = ((a * (a - 1 )) % mod * inv2) % mod;
r = ((b * (b + 1 )) % mod * inv2) % mod;
ret = (r % mod - l % mod);
if (ret < 0 )
ret = ret + mod;
else
ret = ret % mod;
return ret;
}
public static void main(String[] args)
{
String L = "88949273204" ;
String R = "98429729474298592" ;
System.out.println(findSum(L, R));
}
}
|
Python3
mod = 1000000007
inv2 = 500000004 ;
def modulo(num) :
res = 0 ;
for i in range ( len (num)) :
res = (res * 10 + int (num[i]) - 0 ) % mod;
return res;
def findSum(L, R) :
a = modulo(L);
b = modulo(R);
l = ((a * (a - 1 )) % mod * inv2) % mod;
r = ((b * (b + 1 )) % mod * inv2) % mod;
ret = (r % mod - l % mod);
if (ret < 0 ) :
ret = ret + mod;
else :
ret = ret % mod;
return ret;
if __name__ = = "__main__" :
L = "88949273204" ;
R = "98429729474298592" ;
print (findSum(L, R)) ;
|
C#
using System;
class GFG
{
static long mod = 1000000007;
static long inv2 = 500000004;
static long modulo(String num)
{
long res = 0;
for ( int i = 0;
i < num.Length; i++)
res = (res * 10 +
( long )num[i] - '0' ) % mod;
return res;
}
static long findSum(String L, String R)
{
long a, b, l, r, ret;
a = modulo(L);
b = modulo(R);
l = ((a * (a - 1)) % mod * inv2) % mod;
r = ((b * (b + 1)) % mod * inv2) % mod;
ret = (r % mod - l % mod);
if (ret < 0)
ret = ret + mod;
else
ret = ret % mod;
return ret;
}
public static void Main(String[] args)
{
String L = "88949273204" ;
String R = "98429729474298592" ;
Console.WriteLine(findSum(L, R));
}
}
|
Javascript
<script>
let mod = 1000000007;
let inv2 = 500000004;
function modulo(num)
{
let res = 0;
for (let i = 0;
i < num.length; i++)
res = (res * 10 + num[i].charCodeAt() - '0' .charCodeAt()) % mod;
return res;
}
function findSum(L, R)
{
let a, b, l, r, ret;
a = modulo(L);
b = modulo(R);
l = ((a * (a - 1)) % mod * inv2) % mod;
r = ((b * (b + 1)) % mod * inv2) % mod;
ret = (r % mod - l % mod);
if (ret < 0)
ret = ret + mod;
else
ret = ret % mod - 6;
return ret;
}
let L = "88949273204" ;
let R = "98429729474298592" ;
document.write(findSum(L, R));
</script>
|
Time Complexity: O(|L| + |R|)
Auxiliary Space: O(1)