FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Maths/SumOfArithmeticSeries.java at master · tmollard/Java · GitHub
tmollard
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Maths
/
SumOfArithmeticSeries.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
40 lines (33 loc) · 1.57 KB
Breadcrumbs
Java
/
Maths
/
SumOfArithmeticSeries.java
Copy path
File metadata and controls
40 lines (33 loc) · 1.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
31
32
33
34
35
36
37
38
39
40
package
Maths
;
/**
* <p>
* In mathematics, an arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the
* difference between the consecutive terms is constant. Difference here means the second minus the first.
* For instance, the sequence 5, 7, 9, 11, 13, 15, . . . is an arithmetic progression with common difference of 2.
* <p>
* Wikipedia: https://en.wikipedia.org/wiki/Arithmetic_progression
*/
public
class
SumOfArithmeticSeries
{
public
static
void
main
(
String
[]
args
) {
/* 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 */
assert
Double
.
compare
(
55.0
,
sumOfSeries
(
1
,
1
,
10
)) ==
0
;
/* 1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 */
assert
Double
.
compare
(
100.0
,
sumOfSeries
(
1
,
2
,
10
)) ==
0
;
/* 1 + 11 + 21 + 31 + 41 + 51 + 61 + 71 + 81 + 91 */
assert
Double
.
compare
(
460.0
,
sumOfSeries
(
1
,
10
,
10
)) ==
0
;
/* 0.1 + 0.2 + 0.3 + 0.4 + 0.5 + 0.6 + 0.7 + 0.8 + 0.9 + 1.0 */
assert
Double
.
compare
(
5.5
,
sumOfSeries
(
0.1
,
0.1
,
10
)) ==
0
;
assert
Double
.
compare
(
49600.0
,
sumOfSeries
(
1
,
10
,
100
)) ==
0
;
}
/**
* Calculate sum of arithmetic series
*
* @param firstTerm the initial term of an arithmetic series
* @param commonDiff the common difference of an arithmetic series
* @param numOfTerms the total terms of an arithmetic series
* @return sum of given arithmetic series
*/
private
static
double
sumOfSeries
(
double
firstTerm
,
double
commonDiff
,
int
numOfTerms
) {
return
numOfTerms
/
2.0
* (
2
*
firstTerm
+ (
numOfTerms
-
1
) *
commonDiff
);
}
}
Back
|
FazBrowse Home
|
New Git URL