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

javascript - Sum all numbers between two integers - Stack Overflow

programmeradmin1浏览0评论

OBJECTIVE

Given two numbers in an array, sum all the numbers including (and between) both integers (e.g [4,2] -> 2 + 3 + 4 = 9).

I've managed to solve the question but was wondering if there is a more elegant solution (especially using Math.max and Math.min) - see below for more questions...

MY SOLUTION

//arrange array for lowest to highest number
function order(min,max) {
  return min - max;
}


function sumAll(arr) {
  var list = arr.sort(order);
  var a = list[0]; //smallest number
  var b = list[1]; //largest number
  var c = 0;

  while (a <= b) {
    c = c + a; //add c to itself
    a += 1; // increment a by one each time
  }

  return c;
}

sumAll([10, 5]);

MY QUESTION(S)

  1. Is there a more efficient way to do this?
  2. How would I use Math.max() and Math.min() for an array?

OBJECTIVE

Given two numbers in an array, sum all the numbers including (and between) both integers (e.g [4,2] -> 2 + 3 + 4 = 9).

I've managed to solve the question but was wondering if there is a more elegant solution (especially using Math.max and Math.min) - see below for more questions...

MY SOLUTION

//arrange array for lowest to highest number
function order(min,max) {
  return min - max;
}


function sumAll(arr) {
  var list = arr.sort(order);
  var a = list[0]; //smallest number
  var b = list[1]; //largest number
  var c = 0;

  while (a <= b) {
    c = c + a; //add c to itself
    a += 1; // increment a by one each time
  }

  return c;
}

sumAll([10, 5]);

MY QUESTION(S)

  1. Is there a more efficient way to do this?
  2. How would I use Math.max() and Math.min() for an array?
Share Improve this question asked May 21, 2015 at 23:34 jonplacajonplaca 8274 gold badges17 silver badges34 bronze badges 3
  • 1 What you are trying to do is to find the sum of the elements of an arithmetic series. There is a formula for that that does not require looping. – hugomg Commented May 21, 2015 at 23:37
  • elegant way would be to use jquery – Ritesh Karwa Commented May 21, 2015 at 23:37
  • 2 @RiteshK I can't even tell if you're being more serious because you couldn't be more wrong. – Etheryte Commented May 21, 2015 at 23:43
Add a comment  | 

4 Answers 4

Reset to default 23

Optimum algorithm

function sumAll(min, max) {
    return ((max-min)+1) * (min + max) / 2;
}
var array = [4, 2];
var max = Math.max.apply(Math, array); // 4
var min = Math.min.apply(Math, array); // 2

function sumSeries (smallest, largest) {
    // The formulate to sum a series of integers is
    // n * (max + min) / 2, where n is the length of the series.
    var n = (largest - smallest + 1);
    var sum = n * (smallest + largest) / 2; // note integer division

    return sum;
}

var sum = sumSeries(min, max);
console.log(sum);

The sum of the first n integers (1 to n, inclusive) is given by the formula n(n+1)/2. This is also the nth triangular number.

         S1 = 1 + 2 + ... + (a-1) + a + (a+1) + ... + (b-1) + b
            = b(b+1)/2
         S2 = 1 + 2 + ... + (a-1)
            = (a-1)a/2
    S1 - S2 = a + (a+1) + ... + (b-1) + b
            = (b(b+1)-a(a-1))/2

Now we have a general formula for calculating the sum. This will be much more efficient if we are summing a large range (e.g. 1 million to 2 million).

Here is a one liner recursive program solution of SumAll using es6.

const SumAll = (num, sum = 0) =>  num - 1 > 0 ? SumAll(num-1,sum += num) : sum+num;
console.log(SumAll(10));

Note :: Although the best Example is using the Algorithm, as mentioned above. However if the above can be improved.

发布评论

评论列表(0)

  1. 暂无评论