Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. function dblLinear(n) {
  2. const series = [1];
  3.  
  4. const calc = x => ({
  5. y: 2 * x + 1,
  6. z: 3 * x + 1
  7. });
  8.  
  9. const ascendingOrder = (a, b) => a - b;
  10.  
  11. for (let idx = 0; idx <= n; idx++) {
  12. let x = series[idx];
  13. const { y, z } = calc(x);
  14. for (let v of [y, z]) {
  15. if (series.indexOf(v) < 0) {
  16. series.push(v);
  17. series.sort(ascendingOrder);
  18. series.splice(n+1);
  19. }
  20. }
  21. }
  22. return series[n];
  23. }
  24.  
  25. const assert = expr => {
  26. if (!expr) {
  27. throw new Error("Assertion failed.");
  28. }
  29. }
  30.  
  31. assert(dblLinear(10) === 22);
  32. assert(dblLinear(11) === 27);
  33. assert(dblLinear(12) === 28);
  34. assert(dblLinear(90) === 379);
  35. assert(dblLinear(100) === 447);
  36. assert(dblLinear(1000) === 8488);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement