Skip to content

Instantly share code, notes, and snippets.

@thekhairul
Created July 31, 2023 14:30
Show Gist options
  • Save thekhairul/7ab54aa99311c9b9cc6138684467fb95 to your computer and use it in GitHub Desktop.
Save thekhairul/7ab54aa99311c9b9cc6138684467fb95 to your computer and use it in GitHub Desktop.
a function to calculate the sum of the first N natural numbers
function sumOfFirstN(n) {
return (n*(n+1)) / 2;
}
// Time complexity - O(1)
// Space complexity - O(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment