最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to sum all integers below a given integer using recursion - Stack Overflow

programmeradmin2浏览0评论

Right off the bat, this is NOT a homework question. I am practicing recursion problems in my free time and I am still wrapping my head around the concept. I am super close to solving this one, but I can't figure out how to skip the root integer 'n' when I am summing them together. Here is the code so far:

var sumBelow = function (n) {
    console.log(n);
    // base case
    if (n === 0) {
        console.log('we hit the base case');
        return 0;
    }
    // initialize var to hold sum
    if (!sum_sumBelow) var sum_sumBelow = 0;
    // add numbers
    sum_sumBelow = n + sumBelow(n - 1);
    return sum_sumBelow;
};

console.log('answer is', sumBelow(4));

When I call 'sumBelow(4)' what I want is 3+2+1, but I am currently getting 4+3+2+1.

How do I skip the root parameter???

Right off the bat, this is NOT a homework question. I am practicing recursion problems in my free time and I am still wrapping my head around the concept. I am super close to solving this one, but I can't figure out how to skip the root integer 'n' when I am summing them together. Here is the code so far:

var sumBelow = function (n) {
    console.log(n);
    // base case
    if (n === 0) {
        console.log('we hit the base case');
        return 0;
    }
    // initialize var to hold sum
    if (!sum_sumBelow) var sum_sumBelow = 0;
    // add numbers
    sum_sumBelow = n + sumBelow(n - 1);
    return sum_sumBelow;
};

console.log('answer is', sumBelow(4));

When I call 'sumBelow(4)' what I want is 3+2+1, but I am currently getting 4+3+2+1.

How do I skip the root parameter???

Share Improve this question asked Jun 1, 2017 at 3:29 Angela YuAngela Yu 631 gold badge2 silver badges9 bronze badges 3
  • 1 Just have another function "sumbelow'(n)" that calls "sumBelow(n-1)" and call "sumbelow'(4)". – mimre Commented Jun 1, 2017 at 3:31
  • 1 Not what you're asking, but that "initialize var" if statement isn't necessary: the sum_sumBelow variable isn't carried across the recursive calls, it only applies to the current call. You could remove that variable entirely and just return n + sumBelow(n -1). – nnnnnn Commented Jun 1, 2017 at 3:34
  • @nnnnnn thanks for pointing that out! will def keep that in mind – Angela Yu Commented Jun 2, 2017 at 17:58
Add a ment  | 

5 Answers 5

Reset to default 4

Assuming you are printing everything correctly what is wrong with just changing:

sum_sumBelow = n + sumBelow(n - 1);

to

sum_sumBelow = n - 1 + sumBelow(n - 1);

In your example; answer is 6 would be outputted in the console which is 3 + 2 + 1 as you want?

N.B. By no means is this the best recursive solution but it still is one.

Here is an equivalent of your whole function provided by @RobG which uses a ternary:

function sumBelow(n) {return n ? n-1 + sumBelow(n-1) : 0}

The easiest solution would be two separate functions:

function sumUntil(n) {
    return n<=0 ? 0 : n + sumUntil(n-1);
}
function sumBelow(n) {
    return sumUntil(n-1);
}

but you could also decrease all numbers by one:

function sumBelow(n) {
    return n<=1 ? 0 : (n-1) + sumUntil(n-1);
}
// equivalent to
function sumBelow(n) {
    const m = n-1;
    return m<=0 ? 0 : m + sumUntil(m); // make sure not to use m-1 for the recursive call
}

Hey Angela you can skip the root by starting the recursions from the next integer.

var sumBelow = function (n) {
  return sum(n - 1);
};

function sum (n) {
  if (n === 0) return 0;
  return n + sum(n - 1);
}

The idea of recursive function is to executing itself with only the state/parameter we provide repeatedly. So a recursive function itself can't tell what to skip unless we specifically tell it externally. I made a demo so you can try it out.

A little change to your code will meet your idea.

change the line

  sum_sumBelow = n + sumBelow(n - 1);

to

  sum_sumBelow = n + sumBelow(n - 1) - 1 ;

Considering only the first iteration, you are saying

sum_sumBelow = n + sumBelow(n - 1);

As you can see, you are adding n, but you actually want to add 1 less than n. So, instead, you would write

sum_sumBelow = n-1 + sumBelow(n - 1);

Then, on each iteration, you will add 1 less. So instead of 4 + 3 + 2 + 1, you will get 3 + 2 + 1 + 0.

You may want to change the base case to n === 1 instead of 0 so you don't add the zero, but it doesn't really matter.

发布评论

评论列表(0)

  1. 暂无评论