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 LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...