Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

23 lignes
545B

  1. // Approximate square root by the Newton/Raphson method for f(x) = x^2 - n
  2. // f(x) = x^2 - n = 0
  3. // f'(x) = 2x
  4. // x{n+1} = x{n} - (x^2-n) / 2x
  5. def newton ( n )
  6. begin
  7. print "The square root of", n, "is", improve ( n, 1 )
  8. return 0
  9. end
  10. def improve ( n, estimate )
  11. begin
  12. var next
  13. next := estimate - ( (estimate * estimate - n) / ( 2 * estimate ) )
  14. if next - estimate = 0 then
  15. // Integer precision converges at smallest int greater than the square
  16. return next-1
  17. else
  18. return improve ( n, next )
  19. end