-3

i need a different way of writing the addUpTo(n) function . which as a better performance or calculate faster than the function i have below

A JavaScript function that calculates the sum of all numbers from 1 up to (and including) some number n.

function addUpTo(n){
    let total = 0;
    for(let i =1; i<=n; i++){
        total +=i;
    }
    return total;
}

need a faster way of writing this function which is far better than this

Samuel
  • 107
  • 1
  • 1
  • 9
  • "need a faster way of writing this function" : may I ask why ? Now that you already wrote it, you'll never need to write again. You can save it as a library script, or copy paste it in the projects you need. Or do you mean that the function executes faster ? Then, can you be more specific in how this one performs poorly in your scenario (what is your requirement, what is range of inputs possible, how this current implementation fails to satisfy your need, etc..) – Pac0 Oct 06 '19 at 23:03
  • 1
    Check out [this page](https://scotch.io/courses/the-ultimate-guide-to-javascript-algorithms/range-sum) for some cool ideas – faendaro Oct 06 '19 at 23:04
  • 3
    This is the stereotypical problem among stereotypical problems... I can't believe a 30 second search won't give 1000 answers to this. – ASDFGerte Oct 06 '19 at 23:05
  • the current implementation is slower when it comes to timing try this on ur browser console function addUpTo(n){ // let total = 0; // for(let i =1; i<=n; i++){ // total +=i; // } // return total; // } // console.log(addUpTo(4)) //another way function addUpTo(n){ return n * (n+1)/2; } console.log(addUpTo(4)) the 2nd function calculates faster than the 1st – Samuel Oct 06 '19 at 23:06

3 Answers3

4

The sum of the numbers from 1+2+...+N can be expressed as the formula: sum = N*(N+1)/2.

Therefore, you can write:

function addUpTo(n) {
    return n * (n+1) / 2;
}

This will out-perform the method in the question since it removes the loop from the calculation.


See snippet for simple performance analysis

// loop method
function addUpTo(n) {
  let total = 0;
  for(let i =1; i<=n; i++){
    total += i;
  }
  return total;
}

// formula method
function calcSum(n) { return n * (n+1) / 2; }

let t0 = performance.now();
let s1 = addUpTo(1000000);
let t1 = performance.now();

let t2 = performance.now();
let s2 = calcSum(1000000);
let t3 = performance.now();

console.log(`addUpTo calculated ${s1} in: ${t1-t0} ms`);
console.log(`calcSum calculated ${s2} in: ${t3-t2} ms`);
haldo
  • 14,512
  • 5
  • 46
  • 52
1

The sum of the first natural numbers 1,2,...,N can be expressed as N*(N+1)/2.

enter image description here

Thus yielding:

const addUpTo = n => n * (n + 1) / 2;
0
 function addUpTo(n){
     let total = 0;
     for(let i =1; i<=n; i++){
         total +=i;
     }
     return total;
 }

 console.log(addUpTo(4))

//another way

function addUpTo(n){
   return n * (n+1)/2; 
}

console.log(addUpTo(4))         

let t1 = performance.now()

addUpTo(1000000000);
let t2 = performance.now()

console.log(`Time Elapsed ${(t2 - t1)/1000} seconds.`)

if try this on your browser console you discover the 2nd function is faster than the first

Samuel
  • 107
  • 1
  • 1
  • 9