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
>>> 

沒有留言:

張貼留言

WOKWI ESP32 LED Control , Node-Red MQTT SQLITE  

WOKWI ESP32 LED Control ,  Node-Red  MQTT SQLITE   const char broker[] = "test.mosquitto.org" ; //const char broker[] = "br...