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產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...