Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

sqrt

By: phillip1882 on Jun 4th, 2012  |  syntax: Python  |  size: 0.64 KB  |  hits: 86  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import math
  2. def bisection(number):
  3.    a = 1.0
  4.    b = number/2.0
  5.    mid = (a+b)/2
  6.    aprox = number -mid*mid
  7.    while math.fabs(aprox) > 0.0001:
  8.       if aprox < 0:
  9.          b = mid
  10.       else:
  11.          a = mid
  12.       mid = (a+b)/2.0
  13.       aprox = number -mid*mid
  14.    return mid
  15.  
  16. def heron(number):
  17.    aprox = 1.0
  18.    aprox = (aprox +number)/2.0
  19.    while math.fabs(number -aprox*aprox) >0.0001:
  20.       aprox = (aprox +number/aprox)/2.0
  21.    return aprox
  22.  
  23. def newton(number):
  24.    aprox = 1.0
  25.    diff = aprox -number
  26.    while math.fabs(diff) > 0.0001:
  27.       aprox = aprox -(diff)/(2*aprox)
  28.       diff = aprox*aprox -number
  29.    return aprox