Skip to content

Instantly share code, notes, and snippets.

@ajmas
Last active September 4, 2017 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajmas/2ff9c8b9d190064473d068df09ace90f to your computer and use it in GitHub Desktop.
Save ajmas/2ff9c8b9d190064473d068df09ace90f to your computer and use it in GitHub Desktop.
Javascript function, for adding all numbers from 1 to n, inclusive
/**
* Adds all numbers from 1 to maxInteger, inclusive
*/
function addAll (maxInteger) {
let n = maxInteger;
let m = 0;
if (n%2 === 1) {
m = n;
n = n - 1;
}
return (n + n/2 + ((n/2)-1)*n) + m;
}
// See jsfiddle: https://jsfiddle.net/4aesf8oo/1/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment