Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2

In this article, we will learn how to find the sum of a series where the n-th term is defined as n² - (n-1)². We'll explore the mathematical pattern and implement an efficient solution.

Problem Statement

We are given an integer input n and we need to find the sum of all n terms where the n-th term in the series is expressed as ?

Tn = n² - (n-1)²

Understanding the Pattern

Let's first understand what this series looks like by calculating the first few terms ?

# Calculate first few terms to understand the pattern
for n in range(1, 6):
    term = n**2 - (n-1)**2
    print(f"T{n} = {n}² - {n-1}² = {n**2} - {(n-1)**2} = {term}")
T1 = 1² - 0² = 1 - 0 = 1
T2 = 2² - 1² = 4 - 1 = 3
T3 = 3² - 2² = 9 - 4 = 5
T4 = 4² - 3² = 16 - 9 = 7
T5 = 5² - 4² = 25 - 16 = 9

Mathematical Simplification

We can simplify the n-th term using the algebraic identity a² - b² = (a + b)(a - b) ?

# Simplifying n² - (n-1)²
# Using identity: a² - b² = (a + b)(a - b)
# n² - (n-1)² = (n + (n-1))(n - (n-1)) = (2n - 1)(1) = 2n - 1

for n in range(1, 6):
    term_original = n**2 - (n-1)**2
    term_simplified = 2*n - 1
    print(f"n={n}: Original = {term_original}, Simplified = {term_simplified}")
n=1: Original = 1, Simplified = 1
n=2: Original = 3, Simplified = 3
n=3: Original = 5, Simplified = 5
n=4: Original = 7, Simplified = 7
n=5: Original = 9, Simplified = 9

Finding the Sum Formula

Now that we know each term is (2n - 1), the sum of first n terms is ?

# Sum = (2×1 - 1) + (2×2 - 1) + ... + (2×n - 1)
# Sum = 2(1 + 2 + ... + n) - n
# Sum = 2 × n(n+1)/2 - n = n(n+1) - n = n²

def calculate_sum_step_by_step(n):
    # Method 1: Direct summation
    total = sum(2*i - 1 for i in range(1, n+1))
    
    # Method 2: Using formula n²
    formula_result = n * n
    
    print(f"For n = {n}:")
    print(f"Direct summation: {total}")
    print(f"Using formula n²: {formula_result}")
    return formula_result

# Test with small values
for n in [3, 5, 10]:
    calculate_sum_step_by_step(n)
    print()
For n = 3:
Direct summation: 9
Using formula n²: 9

For n = 5:
Direct summation: 25
Using formula n²: 25

For n = 10:
Direct summation: 100
Using formula n²: 100

Efficient Implementation with Modular Arithmetic

For large values of n, we use modular arithmetic to prevent integer overflow ?

def findSum(n):
    """
    Find sum of series where nth term = n² - (n-1)²
    Result: sum = n²
    Using modular arithmetic for large numbers
    """
    mod = 1000000007
    return ((n % mod) * (n % mod)) % mod

# Test with large number
n = 229137999
result = findSum(n)
print(f"Sum for n = {n}: {result}")

# Test with smaller numbers to verify
for test_n in [1, 2, 3, 4, 5]:
    print(f"n = {test_n}: {findSum(test_n)}")
Sum for n = 229137999: 218194447
n = 1: 1
n = 2: 4
n = 3: 9
n = 4: 16
n = 5: 25

Complete Solution

def solve_series_sum(n):
    """
    Complete solution for finding sum of series:
    Tn = n² - (n-1)²
    
    Mathematical proof:
    - Each term simplifies to (2n - 1)
    - Sum of first n terms = n²
    """
    mod = 1000000007
    
    if n <= 0:
        return 0
    
    # For large numbers, use modular arithmetic
    result = ((n % mod) * (n % mod)) % mod
    return result

# Example usage
test_cases = [1, 5, 100, 229137999]

for n in test_cases:
    result = solve_series_sum(n)
    print(f"n = {n}: Sum = {result}")
n = 1: Sum = 1
n = 5: Sum = 25
n = 100: Sum = 10000
n = 229137999: Sum = 218194447

Conclusion

The series with n-th term as n² - (n-1)² simplifies to a sequence of odd numbers (1, 3, 5, 7, ...), and their sum equals n². Using modular arithmetic ensures efficient computation for large values while preventing integer overflow.

Updated on: 2026-03-25T06:30:28+05:30

391 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements