Problem 5. Arithmetic progression
/*Arithmetic Progression
Given first 3 number of a arthimetic progression, predict the next number.
For details about arithmetic progression, you can visit the following
link https://en.wikipedia.org/wiki/Arithmetic_progression
Input
3 integers, each should be taken as a input
Output
single integer
Example
Input:
2
5
8
Output:
11
*/
let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');
function readLine() {
idx++;
return data[idx - 1].trim();
}
//.......................................................................................
// If the intial term of an arithmetic progression is a and the common difference
// of successive members is d , then the n-th term of the sequence (a_n) is given by:
// a_n = a+(n-1)d,
//** Extra information about A.P
//If there are m terms in the A.P , then a_m represents the last term which is given by:
a_m = a +(m-1)d.
let a = parseInt(readLine()); // 2
let b = parseInt(readLine()); //5
let c = parseInt(readLine()); // 8
d = b - a; // or
// let d = b - a; (both are same)
let nthTerm = a + (4 - 1) * d; // here nthTerm is 4 therefore we have used 4
in the formula of A.P
console.log(nthTerm); // 11
Terminal:-
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node Arithmeticprogression.js <input.txt
11
Comments
Post a Comment