-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
16 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
console.log('Arithmetic Progression for non-constant common difference') | ||
|
||
function calculateTerm(n, differences): number { | ||
const a = differences.shift(); // First term | ||
|
||
if (n === 1) { | ||
return a; // Return the first term if n = 1 | ||
} | ||
const sumDifferences = differences.slice(0, n - 1).reduce((sum, d) => sum + d, 0); // Sum up differences up to (n-1) | ||
return a + sumDifferences; | ||
} | ||
const differences = [2, 5, 3, 1, 11, 7, 6, 4]; // Differences | ||
const N_th = 8; // Term to calculate | ||
|
||
const result = calculateTerm(N_th, differences); | ||
console.log(`The ${N_th}th term is ${result}`); |