repl.it
@bearbearmo/

sequence solver-17 lines

Nodejs

No description

fork
loading
Files
  • index.js
  • Packager files
  • package-lock.json
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
let input = require("readline-sync").question("enter your sequence:").split(/, | |,/).map(x => parseFloat(x))
  if (input[1] / input[0] === input[2] / input[1] && input[0] !== input[1]) console.log(input[0] / (input[1] / input[0]) === 1 ? input[1] / input[0] + "^n" : input[0] / (input[1] / input[0]) + "(" + input[1] / input[0] + "^n)");
  else if (input[0] === input[2] && input[1] === input[3] && input[0] !== input[1]) console.log(`${input[0] - ((input[0] - input[1]) / 2) === 0 ? "" : input[0] - ((input[0] - input[1]) / 2) + " + "}${(input[1] - input[0]) / 2 === 1 ? "-1^n" : (input[1] - input[0]) / 2 + "(-1^n)"}`);
  else {
    let nth = "";
    for (let j = 0; true; j = 0) {
      t = [...input];
      for (; (t[0] !== t[t.length - 1] || t[0] !== t[1]) && t.length > 1; t.pop(), j++) for (let i = 0; i < t.length - 1; i++) t[i] = t[i + 1] - t[i];
      if (j === 0) break;
      else {
        for (let i = 1; i <= j; i++) t[0] /= i;
        nth += `${t[0] === 1 ? "" : t[0]}n${j > 1 ? "^" + j : ""} + `;
        input = input.map((x, i) => x - (Math.pow((i + 1), j) * t[0]));
      }
    }
    console.log(nth + t[0]);
  }
Fetching token
?