Calculating the sum of all numbers
≤ n
def add_to(n):
"""
Return the sum of all values from 1 to n.
"""
sum = 0
for i in range(n+1):
sum += i
return sum
What is wrong with the above naive implementation?
The asymptotic performance is
# after refacturing
def add_to(n):
"""
Return the sum of all values from 1 to n.
"""
return n * (n + 1) // 2
Which impact can
time python sum_to.py linear
time python sum_to.py constant
Despite computers being faster than ever, why are data structures and algorithms still important, even more so than in the past?
Because computers have to deal with larger amounts of data.
Computation time costs energy
How many steps are required, at most, for the following tasks?