Advertisement
LightProgrammer000

Raiz Quadrada (metodo Newton)

Jun 17th, 2023
1,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 0.53 KB | None | 0 0
  1. // Bibliotecas
  2. import 'dart:io';
  3.  
  4. // Principal
  5. void main(List<String> args) {
  6.   // Entrada de dados
  7.   stdout.write("# Digite N: ");
  8.   int n = int.parse(stdin.readLineSync()!);
  9.  
  10.   // Chamada de funcao
  11.   print("# Raiz Quadrada de $n: " + raizQuadradaNewton(n).toStringAsFixed(0));
  12. }
  13.  
  14. double raizQuadradaNewton(int n) {
  15.   double p, p_2, b = 2;
  16.  
  17.   while (true) {
  18.     // Calculos
  19.     p = (b + (n / b)) / 2;
  20.     p_2 = p * p;
  21.     b = p;
  22.  
  23.     // Condicional
  24.     if ((-1) * (n - p_2) < 0.001) {
  25.       return p;
  26.     }
  27.   }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement