Menu

gitpiper

sum_of_powers python Code Snippet in 2024

mathintermediate

Last Updated: 17 May 2024

Returns the sum of the powers of all the numbers from start to end (both inclusive).

  • Use range() in combination with a list comprehension to create a list of elements in the desired range raised to the given power.
  • Use sum() to add the values together.
  • Omit the second argument, power, to use a default power of 2.
  • Omit the third argument, start, to use a default starting value of 1.
def sum_of_powers(end, power = 2, start = 1): return sum([(i) ** power for i in range(start, end + 1)])
sum_of_powers(10) # 385 sum_of_powers(10, 3) # 3025 sum_of_powers(10, 3, 5) # 2925

python snippet similar to sum_of_powers For You in May 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️