0

I have this sum:

Obviously, I have to get sum of that depending on what N is. I need to do it in three different ways.

First is for loop:

function lab(n) {
  var S = 0;
  let VS
  if (n == 0) {
    VS = 0;
    return 0;
  }
  if (n == 1) {
    VS = 4;
    return Math.pow(3 / 5, 1);
  } else {
    for (let i = 0; i < n; i++) { //
      S += 1 / n * Math.pow(3 / 5, n);
      t = 4 * n;
    }
    return S;
  }
}

Second one is recursion:

function lab(n) {
  let vs = 0;
  if (n <= 1)
    return 0;
  else {
    vs += 4 * n // vs is how many actions it takes to make this calculation. I’m sure in for loop this is right, but I’m not sure about the recursion approach
    return lab(n - 1) + 1 / n * Math.pow(3 / 5, n)
  }
}

The third way is use recursion with the condition that in order to get S(n) I need to use S(n-1).

I am stuck on this.

Also I get different sums with the same Ns from first and second function.

1 Answer 1

1

I am not sure what you are asking for.

If you are asking for a recursive function then take a look at the following:

function summation(n, sum = 0) {
  if (n <= 0) {
    return sum;
  }
  
  sum += (1/n) * Math.pow(3/5, n);

  return summation(n - 1, sum);
}

console.log(summation(1));
console.log(summation(2));
console.log(summation(3));
console.log(summation(4));
console.log(summation(5));

Another recursive method without passing sum as parameter:

function summation(n) {
  if (n <= 0) {
    return 0;
  }

  return ((1/n) * Math.pow(3/5, n)) + summation(n - 1);
}

console.log(summation(1));
console.log(summation(2));
console.log(summation(3));
console.log(summation(4));
console.log(summation(5));

Also, for the loop method, the following will suffice

function summation(n) {
  var sum = 0;
    
  while (n > 0) {
      sum += (1/n) * Math.pow(3/5, n);
      n -= 1;
  }

  return sum;
}

console.log(summation(1));
console.log(summation(2));
console.log(summation(3));
console.log(summation(4));
console.log(summation(5));

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

5 Comments

These two are great. Also could You tell me how does the number of basic actions change when n is increasing or decreasing. I think for loop is just multiply n by 4, because there are 4 actions (1. / 2. * 3. / 4. ^) so vs=4*n. But how about recurtion?Is it still the same? It's also very confusing for me, because teacher asked us to do simple recursion and recursion that in order to get S(n) i need to use s(n-1), i mean you just did it. Is there any way to do this recursion in different way.
"I think for loop is just multiply n by 4, because there are 4 actions (1. / 2. * 3. / 4. ^) so vs=4*n". I didn't understand what you meant by "basic actions"
That is exactly what I didn't understand, what do you mean by "actions"? I am totally confused by the purpose of VS in your loop. BTW is have aded another recursive method in answer
Sorry, for confusion! I'll try my best. So by saying action i mean add, subtract, divide, pow etc. So for example this : (1/n) * Math.pow(3/5, n) has 4 action s right? first we divide, then multiply then we divide again and then we pow it. action = mathematical action. so for example in loop function if n=2, then total methematical actions = 8
Oh yes I suppose but the actual order will be division, division, the power and then finally the multiplication. While I don't know why you need to keep track of these actions, but just passing it as parameter will be sufficient in the case of recursion which will increase by 4 in each call.

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.