fork download
  1. #!/usr/bin/python
  2. # coding:utf-8
  3. from __future__ import print_function, unicode_literals
  4. import sys
  5.  
  6.  
  7. N = 0.01
  8.  
  9.  
  10. def main():
  11. # input
  12. print("Input a number for square root calculation.")
  13. try:
  14. input = sys.stdin.readline().strip("\n")
  15. except KeyboardInterrupt:
  16. sys.exit()
  17. try:
  18. input = float(input)
  19. except ValueError:
  20. sys.exit("input error")
  21.  
  22. # calculate and output
  23. print("loop: "+str(sb_sqrt(input)))
  24. print("recursive: "+str(sb_sqrt2(input)))
  25.  
  26.  
  27. def sb_sqrt(target):
  28. """square root calculation by loop.
  29. """
  30. r = N
  31. while r * r < target:
  32. r += N
  33. # r -= N
  34.  
  35. return r
  36.  
  37.  
  38. def sb_sqrt2(target):
  39. """square root calculation by recursive and clojure.
  40. """
  41. def _(r):
  42. # print("deb1:"+str(r))
  43. if r * r < target:
  44. # print("deb2:"+str(r))
  45. return _(r+N)
  46. # print("deb3:"+str(r))
  47. else:
  48. # print("deb4:"+str(r))
  49. return r
  50.  
  51. return _(N)
  52.  
  53.  
  54. if __name__ == "__main__":
  55. main()
  56.  
Success #stdin #stdout 0.01s 6652KB
stdin
10.0
stdout
Input a number for square root calculation.
loop: 3.17
recursive: 3.17