fork download
  1. # divide et impera
  2. def __summa(lo, hi, arr):
  3.  
  4. if lo == hi:
  5. return arr[lo]
  6. else:
  7. m = (lo + hi) >> 1
  8. a = __summa(lo, m, arr)
  9. b = __summa(m + 1, hi, arr)
  10. s = a + b
  11. return s
  12.  
  13. # loop standard
  14. def _summa(arr):
  15. s = 0
  16. for i in range(0, len(arr)):
  17. s = s + arr[i]
  18. return s
  19.  
  20. # head and tail list
  21. def summa(arr):
  22. if arr == []:
  23. return 0
  24. else:
  25. return arr[0] + summa(arr[1:])
  26. def main():
  27. arr = [1,2,3,4,5,6,7,8,9,10]
  28. n = len(arr)
  29. s = __summa(0, len(arr) - 1, arr)
  30. print(s)
  31. print(s == summa(arr) == _summa(arr) == n * (n + 1 )//2)
  32. main()
Success #stdin #stdout 0.02s 9136KB
stdin
Standard input is empty
stdout
55
True