Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function dblLinear(n) {
- const series = [1];
- const calc = x => ({
- y: 2 * x + 1,
- z: 3 * x + 1
- });
- const ascendingOrder = (a, b) => a - b;
- for (let idx = 0; idx <= n; idx++) {
- let x = series[idx];
- const { y, z } = calc(x);
- for (let v of [y, z]) {
- if (series.indexOf(v) < 0) {
- series.push(v);
- series.sort(ascendingOrder);
- series.splice(n+1);
- }
- }
- }
- return series[n];
- }
- const assert = expr => {
- if (!expr) {
- throw new Error("Assertion failed.");
- }
- }
- assert(dblLinear(10) === 22);
- assert(dblLinear(11) === 27);
- assert(dblLinear(12) === 28);
- assert(dblLinear(90) === 379);
- assert(dblLinear(100) === 447);
- assert(dblLinear(1000) === 8488);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement