Definition
- Loop version – seed sum at 0, add each counter value from 1 to N; O(n) time.
- Closed form (Gauss) –
N * (N + 1) / 2gives the sum in O(1) time. - Exact division – one of N and N+1 is even, so dividing by 2 is always exact with no remainder.
- Overflow caution – for large N,
N * (N + 1)can overflow; useBigIntegeror divide before multiplying. - Recognizing closed forms – replacing a loop with a formula turns O(n) into O(1).
National Bank of India projects cumulative interest accrual over 10 periods where period k contributes k units. The total is 1+2+...+10 = 55, or instantly by the formula 10 * 11 / 2 = 55.
Understanding the Concepts
What is the problem?
Calculate and print 1 + 2 + 3 + ... + 10 = 55. No external input is needed.
How do you implement the loop version?
package com.bank.service;
public class SeriesSum {
public static long sumLoop(int n) {
long sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
public static long sumFormula(int n) {
return (long) n * (n + 1) / 2;
}
public static void main(String[] args) {
System.out.println("Loop: " + sumLoop(10));
System.out.println("Formula: " + sumFormula(10));
}
}
Loop: 55
Formula: 55
Why is the formula exact?
N * (N + 1) is a product of two consecutive integers. One of them must be even, so the product is always even, and dividing by 2 is always exact with no remainder. For 10: 10 * 11 = 110, and 110 / 2 = 55 exactly.
How is the formula derived?
Write the sum forward and backward, then add them vertically:
S = 1 + 2 + 3 + ... + N
S = N + (N-1) + (N-2) + ... + 1
-----
2S = (N+1) + (N+1) + ... + (N+1) [N terms]
2S = N * (N + 1)
S = N * (N + 1) / 2
This pairing trick is attributed to Gauss. Each pair sums to N+1 and there are N such pairs, giving 2S = N*(N+1).
Why use long or BigInteger?
For large N, N * (N + 1) can overflow an int (max ~2.1 billion). Casting to long first avoids this.
// Wrong: int overflow for large n
int sum = n * (n + 1) / 2; // overflows when n > ~46340
// Correct: cast to long
long sum = (long) n * (n + 1) / 2;
For very large N, use BigInteger:
import java.math.BigInteger;
BigInteger sum = BigInteger.valueOf(n)
.multiply(BigInteger.valueOf(n + 1))
.divide(BigInteger.TWO);
How does this differ from summing external inputs?
In the previous problem, the loop reads external values. Here, the thing being summed is the loop counter itself. The loop mechanics are identical: seed at 0, accumulate, return. The difference is the source of the values.
What about related series formulas?
Several common series have closed forms that turn O(n) loops into O(1) computations:
| Series | Formula |
|---|---|
| 1 + 2 + ... + N | N*(N+1)/2 |
| 2 + 4 + ... + 2N (first N evens) | N*(N+1) |
| 1 + 3 + ... + (2N-1) (first N odds) | N*N |
| 1^2 + 2^2 + ... + N^2 | N*(N+1)*(2N+1)/6 |
What are edge cases?
- N = 0: sum is 0 (loop does not execute, formula gives 0).
- N = 1: sum is 1.
- N negative: undefined for natural numbers; return 0 or throw.
What are common mistakes?
// Wrong: forgetting to cast to long
int n = 100000;
int result = n * (n + 1) / 2; // overflow!
// Correct
long result = (long) n * (n + 1) / 2;
How is this used in a banking projection?
At National Bank of India, a projection engine computed cumulative accrual over N periods where period k contributed k units of interest. For 10 periods, the total accrual is 55 units. The closed form replaced the loop for speed.
int periods = 10;
long totalAccrual = (long) periods * (periods + 1) / 2;
System.out.println("Total accrual units: " + totalAccrual); // 55
What should a senior engineer know?
Recognizing when a loop can be replaced by a closed form is a key optimization skill. The sum of an arithmetic series has a formula. The sum of a geometric series has a formula. Factorials, Fibonacci, and power sums each have their own. A senior engineer tries the formula first and falls back to a loop only when no closed form exists.
What are common interview follow-up questions?
- What is the formula for the sum of the first N even numbers? (N*(N+1))
- What is the formula for the sum of squares 1^2 + 2^2 + ... + N^2? (N*(N+1)*(2N+1)/6)
- Can you prove the formula N*(N+1)/2 by induction?
Visual Overview
Loop version:
sum=0, i=1: sum=1
i=2: sum=3
i=3: sum=6
i=4: sum=10
i=5: sum=15
i=6: sum=21
i=7: sum=28
i=8: sum=36
i=9: sum=45
i=10: sum=55
Formula: 10 * 11 / 2 = 55
Key Takeaway
The sum of the first N natural numbers is an accumulation loop where the thing being summed is the loop counter itself. Seed at 0, add each counter value from 1 to N, and the result for 10 is 55.
The more valuable interview answer is the closed form: N * (N + 1) / 2, which computes the answer in constant time. The division is always exact because N * (N + 1) is a product of consecutive integers, one of which is even. Spotting when a loop can be replaced by a formula is the optimization that turns O(n) into O(1).
For large N, the intermediate product can overflow an int, so cast to long or use BigInteger. The formula itself is simple, but the overflow trap is real.
In a banking project at National Bank of India, a projection engine computed cumulative accrual over N periods where period k contributed k units. The closed form replaced the loop for speed, and BigInteger kept it exact for large period counts.
Remember one sentence: the sum of 1 to N is N * (N + 1) / 2, computed in constant time, and the division is always exact.
---