Skip to content

Instantly share code, notes, and snippets.

@gargeesuresh
Created March 7, 2023 03:27
Show Gist options
  • Select an option

  • Save gargeesuresh/1c4ff8389b89c43491c6bf6c5fe18ded to your computer and use it in GitHub Desktop.

Select an option

Save gargeesuresh/1c4ff8389b89c43491c6bf6c5fe18ded to your computer and use it in GitHub Desktop.
# too hot
def sum_of_n_numbers(n):
return reduce(lambda acc, x: acc + x, map(lambda x: x[1], filter(lambda x: x[0] % 2 == 0, zip(range(1, n+1), sum_of_n_numbers(n-1) if n > 1 else []))), 0)
# too cold
def sum_of_n_numbers(n):
total = 0
for i in range(1, n+1):
total += i
return total
# just right
def sum_of_n_numbers(n):
return n * (n + 1) // 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment