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

沒有留言:

張貼留言

MQTT Explorer 與 Node-Red 介面的實驗

 MQTT Explorer 與 Node-Red 介面的實驗 MQTT EXplorer 與 Node-Red介面的設定 (1) 設定  MQTT EXplorer Client    (2)        Node-Red LED ON  --> MQTT Explor...