Como fazer a raiz quadrada de um numero em javascript

Ahmed Samir on May 14, 2022

The Connect 4 Game 

The purpose of this assignment is to create a working text-mode version of the popular game, Conn...

Page 2

Ahmed Samir on May 14, 2022

The Connect 4 Game 

The purpose of this assignment is to create a working text-mode version of the popular game, Conn...

Page 3

Ahmed Samir on May 14, 2022

The Connect 4 Game 

The purpose of this assignment is to create a working text-mode version of the popular game, Conn...

Page 4

Grepper Account Login Required

Dado um número N , a tarefa é encontrar a raiz quadrada de N sem usar a função sqrt() .

Exemplos: 

Entrada: N = 25 
Saída: 5

Entrada: N = 3 
Saída: 1,73205

Entrada: N = 2,5 
Saída: 1,58114 
 



Abordagem:  

  • Comece iterando de i = 1. Se i * i = n , então imprima i como n é um quadrado perfeito cuja raiz quadrada é i .
  • Caso contrário, encontre o menor i para o qual i * i é estritamente maior que n .
  • Agora sabemos raiz quadrada de n mentiras no intervalo i - 1 e i e podemos usar Binary algoritmo de busca para encontrar a raiz quadrada.
  • Encontrar meados de i - 1 e i e comparar meados * mid com n , com precisão de até 5 casas decimais. 
    1. Se mid * mid = n então retorna mid .
    2. Se mid * mid <n, então ocorre novamente na segunda metade.
    3. Se mid * mid> n, então, ocorre novamente na primeira metade.

Abaixo está a implementação da abordagem acima:  

// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Recursive function that returns square root // of a number with precision upto 5 decimal places double Square(double n, double i, double j) { double mid = (i + j) / 2; double mul = mid * mid; // If mid itself is the square root, // return mid if ((mul == n) || (abs(mul - n) < 0.00001)) return mid; // If mul is less than n, recur second half else if (mul < n) return Square(n, mid, j); // Else recur first half else return Square(n, i, mid); } // Function to find the square root of n void findSqrt(double n) { double i = 1; // While the square root is not found bool found = false; while (!found) { // If n is a perfect square if (i * i == n) { cout << fixed << setprecision(0) << i; found = true; } else if (i * i > n) { // Square root will lie in the // interval i-1 and i double res = Square(n, i - 1, i); cout << fixed << setprecision(5) << res; found = true; } i++; } } // Driver code int main() { double n = 3; findSqrt(n); return 0; } // Java implementation of the approach import java.util.*; class GFG { // Recursive function that returns // square root of a number with // precision upto 5 decimal places static double Square(double n, double i, double j) { double mid = (i + j) / 2; double mul = mid * mid; // If mid itself is the square root, // return mid if ((mul == n) || (Math.abs(mul - n) < 0.00001)) return mid; // If mul is less than n, // recur second half else if (mul < n) return Square(n, mid, j); // Else recur first half else return Square(n, i, mid); } // Function to find the square root of n static void findSqrt(double n) { double i = 1; // While the square root is not found boolean found = false; while (!found) { // If n is a perfect square if (i * i == n) { System.out.println(i); found = true; } else if (i * i > n) { // Square root will lie in the // interval i-1 and i double res = Square(n, i - 1, i); System.out.printf("%.5f", res); found = true; } i++; } } // Driver code public static void main(String[] args) { double n = 3; findSqrt(n); } } // This code is contributed by PrinciRaj1992 # Python3 implementation of the approach import math # Recursive function that returns square root # of a number with precision upto 5 decimal places def Square(n, i, j): mid = (i + j) / 2; mul = mid * mid; # If mid itself is the square root, # return mid if ((mul == n) or (abs(mul - n) < 0.00001)): return mid; # If mul is less than n, recur second half elif (mul < n): return Square(n, mid, j); # Else recur first half else: return Square(n, i, mid); # Function to find the square root of n def findSqrt(n): i = 1; # While the square root is not found found = False; while (found == False): # If n is a perfect square if (i * i == n): print(i); found = True; elif (i * i > n): # Square root will lie in the # interval i-1 and i res = Square(n, i - 1, i); print ("{0:.5f}".format(res)) found = True i += 1; # Driver code if __name__ == '__main__': n = 3; findSqrt(n); # This code is contributed by 29AjayKumar // C# implementation of the approach using System; class GFG { // Recursive function that returns // square root of a number with // precision upto 5 decimal places static double Square(double n, double i, double j) { double mid = (i + j) / 2; double mul = mid * mid; // If mid itself is the square root, // return mid if ((mul == n) || (Math.Abs(mul - n) < 0.00001)) return mid; // If mul is less than n, // recur second half else if (mul < n) return Square(n, mid, j); // Else recur first half else return Square(n, i, mid); } // Function to find the square root of n static void findSqrt(double n) { double i = 1; // While the square root is not found Boolean found = false; while (!found) { // If n is a perfect square if (i * i == n) { Console.WriteLine(i); found = true; } else if (i * i > n) { // Square root will lie in the // interval i-1 and i double res = Square(n, i - 1, i); Console.Write("{0:F5}", res); found = true; } i++; } } // Driver code public static void Main(String[] args) { double n = 3; findSqrt(n); } } // This code is contributed by Princi Singh <script> // Javascript implementation of the approach // Recursive function that returns // square root of a number with // precision upto 5 decimal places function Square(n, i, j) { var mid = ((i + j) / 2); var mul = mid * mid; // If mid itself is the square root, // return mid if ((mul == n) || (Math.abs(mul - n) < 0.00001)) return mid; // If mul is less than n, // recur second half else if (mul < n) return Square(n, mid, j); // Else recur first half else return Square(n, i, mid); } // Function to find the square root of n function findSqrt(n) { var i = 1; // While the square root is not found var found = false; while (!found) { // If n is a perfect square if (i * i == n) { document.write(i); found = true; } else if (i * i > n) { // Square root will lie in the // interval i-1 and i var res = Square(n, i - 1, i); document.write(res.toFixed(5)); found = true; } i++; } } // Driver code var n = 3; findSqrt(n); // This code is contributed by todaysgaurav </script>

Última postagem

Tag