-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGauß_needs_help.js
19 lines (12 loc) · 1.03 KB
/
Gauß_needs_help.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
Due to another of his misbehaved, the primary school's teacher of the young Gauß, Herr J.G. Büttner, to keep the bored and unruly young schoolboy Karl Friedrich Gauss busy for a good long time, while he teaching arithmetic to his mates, assigned him the problem of adding up all the whole numbers from 1 through a given number n.
Your task is to help the young Carl Friedrich to solve this problem as quickly as you can; so, he can astonish his teacher and rescue his recreation interval.
Here's, an example:
f(n=100) // returns 5050
It's your duty to verify that n is a valid positive integer number. If not, please, return false (None for Python, null for C#, 0 for COBOL).
Note: the goal of this kata is to invite you to think about some 'basic' mathematic formula and how you can do performance optimization on your code.
Advanced - experienced users should try to solve it in one line, without loops, or optimizing the code as much as they can.
*/
function f(n){
return Number.isInteger(n) && n > 0 ? n * (n + 1) / 2 : false;
};