-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution19.py
More file actions
27 lines (22 loc) · 745 Bytes
/
solution19.py
File metadata and controls
27 lines (22 loc) · 745 Bytes
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
"""
# ex.19
Create a program that reads a number that you want to get the sum until that number
Create a program that:
* Reads the number you want to sum
* Calculates the sum of 1+2+3+4...+98+99+n
* Prints the sum of 1+2+3+4...+98+99+n
* Input example: 100
* Output: "The sum is 5050"
###### Try the program with a very very big number, [if it takes too long check this
out](http://mathcentral.uregina.ca/qq/database/qq.02.06/jo1.html)
"""
from timeit import default_timer as timer
start = timer()
def sum_of_natural_numbers(last_number):
sum = last_number* (1 + last_number) / 2
return int(sum)
num = 2000000202020202020202020202
result = sum_of_natural_numbers(num)
print(f"The sum is {result}")
stop = timer()
print(stop-start)