Series summation if T(n) is given and n is very large

Given a sequence whose nth term is

T(n) = n2 – (n – 1)2

The task is to evaluate the sum of first n terms i.e.



S(n) = T(1) + T(2) + T(3) + … + T(n)

Print S(n) mod (109 + 7).

Examples:

Input: n = 3
Output: 9
S(3) = T(1) + T(2) + T(3) = (12 – 02) + (22 – 12) + (32 – 22) = 1 + 3 + 5 = 9

Input: n = 10
Output: 100

Approach: If we try to find out some initial terms of the sequence by putting n = 1, 2, 3, … in T(n) = n2 – (n – 1)2, we find the sequence 1, 3, 5, …
Hence, we find an A.P. where first term is 1 and d (common difference between consecutive
terms) is 2.
The formula for the sum of n terms of A.P is

S(n) = n / 2 [ 2 * a + (n – 1) * d ]

where a is the first term.
So, putting a = 1 and d = 2, we get

S(n) = n2

.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007
  
// Function to return the sum
// of the given series
int sumOfSeries(int n)
{
    ll ans = (ll)pow(n % MOD, 2);
  
    return (ans % MOD);
}
  
// Driver code
int main()
{
    int n = 10;
    cout << sumOfSeries(n);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
      
public static final int MOD = 1000000007;
  
// Function to return the sum
// of the given series
static int sumOfSeries(int n)
{
    int ans = (int)Math.pow(n % MOD, 2);
  
    return (ans % MOD);
}
  
// Driver code
public static void main(String[] args)
{
    int n = 10;
    System.out.println(sumOfSeries(n));
}
}
  
// This code is contributed by Code_Mech.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 implementation of the approach
from math import pow
  
MOD = 1000000007
  
# Function to return the sum
# of the given series
def sumOfSeries(n):
    ans = pow(n % MOD, 2)
  
    return (ans % MOD)
  
# Driver code
if __name__ == '__main__':
    n = 10
    print(int(sumOfSeries(n)))
  
# This code is contributed by
# Surendra_Gangwar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
      
const int MOD = 1000000007;
  
// Function to return the sum
// of the given series
static int sumOfSeries(int n)
{
    int ans = (int)Math.Pow(n % MOD, 2);
  
    return (ans % MOD);
}
  
// Driver code
public static void Main()
{
    int n = 10;
    Console.Write(sumOfSeries(n));
}
}
  
// This code is contributed 
// by Akanksha Rai

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of the approach 
$GLOBALS['MOD'] = 1000000007; 
  
// Function to return the sum 
// of the given series 
function sumOfSeries($n
    $ans = pow($n % $GLOBALS['MOD'], 2); 
  
    return ($ans % $GLOBALS['MOD']); 
  
// Driver code 
$n = 10; 
echo sumOfSeries($n); 
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

100


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.