Post

Created by @nathanedwards
 at November 1st 2023, 2:41:47 am.

Question:

A math class is learning about different methods in mathematics. The teacher gives the students the following problem:

The sum of the first 10 terms of an arithmetic series is 130. The common difference between the terms is 5. Find the first term of the series.

Write a method in Java named findFirstTerm that takes in an integer sum and an integer diff, representing the sum of the first n terms of an arithmetic series and the common difference between the terms respectively, and returns the first term of the series. Assume that the sum and the difference are positive integers.

Use the formula for the sum of an arithmetic series:

sum = (n / 2)(2a + (n - 1)d)

where sum is the sum of the series, n is the number of terms, a is the first term, and d is the common difference.

Your method should have the following signature: public static int findFirstTerm(int sum, int diff)

Example:

Input:

sum = 130
diff = 5

Output:

25

Explanation:

Using the formula for the sum of an arithmetic series, we can rearrange it to solve for a:

sum = (n / 2)(2a + (n - 1)d)

Rearranging the equation:

sum = n(a + a + (n - 1)d) / 2

Multiplying both sides by 2 to eliminate the fraction:

2sum = n(2a + (n - 1)d)

Expanding the terms:

2sum = 2an + nd - nd + d
2sum = 2an + d(n - 1)

Rearranging again:

2an = 2sum - d(n - 1)

Finally, solving for a:

a = (2sum - d(n - 1)) / (2n)

Substituting the given values:

a = (2 * 130 - 5(10 - 1)) / (2 * 10)
a = (260 - 5(9)) / 20
a = (260 - 45) / 20
a = 215 / 20
a = 10.75

Since the common difference and the number of terms are both positive integers, the first term a must also be an integer. Therefore, the first term is 25.

Java Solution:

public class Main {
    public static void main(String[] args) {
        int sum = 130;
        int diff = 5;

        int firstTerm = findFirstTerm(sum, diff);
        System.out.println(firstTerm);
    }
    
    public static int findFirstTerm(int sum, int diff) {
        int numTerms = sum / diff;
        int firstTerm = (2 * sum - diff * (numTerms - 1)) / (2 * numTerms);
        return firstTerm;
    }
}