2018年1月11日 星期四

Python: Function that takes a positive integer and returns the sum of the cube of all the positive integers smaller than the specified number

Python: Function that takes a positive integer and returns the sum of the cube of all the positive integers smaller than the specified number

Python Basic: Exercise-149 with Solution

Write a Python function that takes a positive integer and returns the sum of the cube of all the positive integers smaller than the specified number.
Sample Solution :-
def sum_of_cubes(n):
  n -= 1
  total = 0
  while n > 0:
    total += n * n * n
    n -= 1
  return total

a=3
print("Sum of cubes: ",sum_of_cubes(a+1))

#1+8+27=36


=========== RESTART: F:/Python_APSC/py-ex-basic-149.py ==========
Sum of cubes:  36
>>> 

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...