-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.py
26 lines (18 loc) · 1.17 KB
/
solution.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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#).
# 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.
# Credits: this kata was inspired by the farzher's kata 'Sum of large ints' .
# In fact, it can be seen as a sort of prep kata for that one.
def f(n):
return None if not isinstance(n, int) or n <= 0 else n * (n + 1) // 2