Last active
September 4, 2017 20:51
-
-
Save ajmas/2ff9c8b9d190064473d068df09ace90f to your computer and use it in GitHub Desktop.
Javascript function, for adding all numbers from 1 to n, inclusive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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