Skip to content

Commit

Permalink
Create ArithmeticProgression.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
godwinjs authored Jan 15, 2025
1 parent bf7b3bb commit 80c1860
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Maths/ArithmeticProgression.ts
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}`);

0 comments on commit 80c1860

Please sign in to comment.