forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometricProgression.java
More file actions
30 lines (27 loc) · 941 Bytes
/
GeometricProgression.java
File metadata and controls
30 lines (27 loc) · 941 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
30
package com.examplehub.maths;
public class GeometricProgression {
/**
* Get nth item of geometric progression.
*
* @param firstItem the first item of geometric progression.
* @param commonRatio the common ratio of geometric progression.
* @param nth the nth.
* @return the nth item of geometric progression.
*/
public static double getNth(double firstItem, double commonRatio, int nth) {
return firstItem * Math.pow(commonRatio, nth - 1);
}
/**
* Sum of geometric progression.
*
* @param firstItem the first item of geometric progression.
* @param commonRatio the common ratio of geometric progression.
* @param nth the nth.
* @return the sum of geometric progression.
*/
public static double sum(double firstItem, double commonRatio, int nth) {
return commonRatio == 1
? firstItem * nth
: firstItem * (1 - Math.pow(commonRatio, nth)) / (1 - commonRatio);
}
}