1

I'm trying to teach myself recursion and I seem to be at a total loss on how to code this particular recursive sequence. I always end up going in some bruteforcing (even hardcoding) ways that lead to nowhere.

I have start which is my starting input (the first member will be equal to start). start = 2 for example.

The sequence is:

member1 = start;
member2 = member1 + 1;
member3 = 2 * member1 + 1;
member4 = member1 + 2;
member5 = member2 + 1;
member6 = 2 * member2 + 1;
member7 = member2 + 2;

I have to return the N-th member of this sequence. N can be any number.. for example N = 11. So then I need to return 11th member of this sequence. How do I implement this recursively?

I really appreciate your help in advance!

1
  • 1
    can you add the sequence of numbers with a start value of one? Commented Mar 19, 2021 at 9:29

2 Answers 2

2

You could take a zero based approach by returning the start value at position zero, like for sequences

a0 = start

For all other values divide the nth by three and return the result of wanted operation.

n    operation                     an  reference
0    member1 = start;               1

1    member2 = member1 + 1;         2      a0
2    member3 = 2 * member1 + 1;     3      a0
3    member4 = member1 + 2;         3      a0

4    member5 = member2 + 1;         3      a1
5    member6 = 2 * member2 + 1;     5      a1
6    member7 = member2 + 2;         4      a1

function getSequence(start, n) {
    if (!n--) return start;
    const value = getSequence(start, Math.floor(n / 3));
    switch (n % 3) {
        case 0: return value + 1;
        case 1: return value * 2 + 1;
        case 2: return value + 2;
    }
}

console.log(...Array.from({ length: 10 }, (_, i) => getSequence(1, i)));
console.log(...Array.from({ length: 10 }, (_, i) => getSequence(2, i)));

One approach

function getSequence(start, n) {
    if (!--n) return start;
    const value = getSequence(start, 1 + Math.floor(--n / 3));
    switch (n % 3) {
        case 0: return value + 1;
        case 1: return value * 2 + 1;
        case 2: return value + 2;
    }
}

console.log(...Array.from({ length: 10 }, (_, i) => getSequence(1, i + 1)));
console.log(...Array.from({ length: 10 }, (_, i) => getSequence(2, i + 1)));

Sign up to request clarification or add additional context in comments.

Comments

0

As stated by @mplungjan, your question isn't very clear.
From what i'm understanding, you'd want a function that takes in any number (start in your original example) and a position (end) and gives you back its result following your pattern, which, again from what I understood, is :
x + 1
x * 2 + 1
x + 2

If all my assumptions are correct, a function like follows should work

function getNthElement(start, end) {
  let result = start
  for ( i = 0; i < end; i++) {
    if (i % 3 === 0) { // first step
      result = result + 1
    }
    if (i % 3 === 1) { // second step
      result = result * 2 + 1
    }
    if (i % 3 === 2) { //third step
      result = result + 2
    }
  }
  return result
}


console.log(getNthElement(1, 10))

1 Comment

If have not checked if the outcome is correct, but the asker asked specifically for help with recursion, which this solution is not using.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.