fork(1) download
  1. # dividing without divide
  2.  
  3. def divide(num, div):
  4. quot = 0
  5. while div <= num:
  6. quot += 1
  7. num -= div
  8. return quot, num
  9.  
  10. print divide(31,5)
  11.  
  12. def divide(num, div):
  13. divs, sqs, sum = [div], [1], 0
  14. while divs[-1] * divs[-1] < num:
  15. divs.append(divs[-1] * divs[-1])
  16. sqs.append(sqs[-1] * sqs[-1] * div)
  17. while divs:
  18. div, sq = divs.pop(), sqs.pop()
  19. print div, sq,
  20. quot = 0
  21. while div <= num:
  22. quot += 1
  23. num -= div
  24. sum += (quot * sq)
  25. print (quot * sq)
  26. return sum, num
  27.  
  28. print divide(1000000000,3)
  29. print divide(31,5)
Success #stdin #stdout 0s 23296KB
stdin
Standard input is empty
stdout
(6, 1)
43046721 14348907 330024861
6561 2187 3306744
81 27 1728
9 3 0
3 1 0
(333333333, 1)
25 5 5
5 1 1
(6, 1)