forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmeticProgression.java
More file actions
29 lines (26 loc) · 959 Bytes
/
ArithmeticProgression.java
File metadata and controls
29 lines (26 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.examplehub.maths;
/** https://en.wikipedia.org/wiki/Arithmetic_progression */
public class ArithmeticProgression {
/**
* Get nth item.
*
* @param firstItem the initial term of an arithmetic progression.
* @param commonDifference the common difference of successive members.
* @param nth the nth term of the sequence.
* @return the nth tern.
*/
public static int getNth(int firstItem, int commonDifference, int nth) {
return firstItem + (nth - 1) * commonDifference;
}
/**
* Sum of n items of arithmetic progression.
*
* @param firstItem the initial term of an arithmetic progression.
* @param commonDifference the common difference of successive members.
* @param n the numbers of arithmetic progression.
* @return sum of arithmetic progression.
*/
public static int sum(int firstItem, int commonDifference, int n) {
return (2 * firstItem + (n - 1) * commonDifference) * n / 2;
}
}