Search code examples
javarecursionjgrasp

Is there a way to make a sqrt function without using Math.sqrt ()and without loops?


I'm trying to make program that uses recursive for a college homework but teacher does not want me to use loops for the problems but this is a jGRASP java program. And this is the homework:homework attachment

My code

I tried this code: my code

import java.util.Scanner;

public class SquareRoot {
 public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   System.out.println("Enter a number: ");
   int x = scanner.nextInt();
   System.out.println("Enter a number: ");
   int y = scanner.nextInt();
   
   System.out.println("" + powS(x));
   System.out.println("" + powNS(y));
 }
 
 public static int powS(int x) {
   int result = (2 * x) - 1;
   if (x == 0) {
     return 0;
   }
   return result + powS(x-1);
 }
 
 public static int powNS(int y) {
   int solution = (2 * y) - 1;
   if (y == 0) {
     return 0;
   }
   return solution + powS(y-1);
 }
}

And the square function worked but the only problem is that I can't figure out how to change the last method to make a square root function without loops(no while loops and no for loops)

I tried this code: my code

import java.util.Scanner;

public class SquareRoot9 {
 public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   System.out.println("Enter a number: ");
   int x = scanner.nextInt();
   System.out.println("Enter a number: ");
   int y = scanner.nextInt();
   
   System.out.println("" + powS(x));
   System.out.println("" + powNS(y));
 }
 
 public static int powS(int x) {
   int result = 2 * (x-1);
   if (x == 0) {
     return 0;
   }
   return result * powS(x-1);
 }
 
 public static int powNS(int y) {
   int solution = 2 * (y-1);
   if (y == 0) {
     return 1;
   }
   if (y == 1) {
     return solution;
   }
   return solution / powNS(y-1);
 }
}

but there's an error

And then I tried this code: [my code]

import java.util.Scanner;

public class SquareRoot10 {
 public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   System.out.println("Enter a number: ");
   int x = scanner.nextInt();
   System.out.println("Enter a number: ");
   int y = scanner.nextInt();
   
   System.out.println("" + powS(x));
   System.out.println("" + powNS(y));
 }
 
 public static int powS(int x) {
   int result = (2 * x) - 1;
   if (x == 0) {
     return 0;
   }
   return result + powS(x-1);
 }
 
 public static int powNS(int y) {
   int solution = (2 / y) - 1;
   if (y == 0) {
     return 0;
   }
   return solution + powS(y-1);
 }
}

(https://i.sstatic.net/2ZRQV.png) and it worked but the code didn't retrieve the answer I'm looking for and the code says I got 625 when I was looking for the square root of the number.

Does anyone know how to solve or change the last method so I can get the square root of a number for a jGRASP java program?


Solution

  • Is there a way to make a sqrt function without using Math.sqrt ()and without loops? Yes.

    You can use Newton's method in combination with recursion.

    Consider if you want to find the sqrt(10). Then the equation f(x) = x2 - 10 will solve for x when f(x) = 0.

    The first derivative of f(x) is f'(x) = 2*x. So read the wiki page apply x, f(x), and f'(x) as described.

    • first do it in a loop to ensure you have the algorithm down.
    • then do it recursively to fulfill the requirements.
    • in both cases you should stop the iteration when |xn+1 - xn| are within a reasonable tolerance.
    • finally, you will want to use some variable in place of 10 in the above mentioned equations to take the sqrt root of any number.