Square Root in Java: How to Find Square Root in Java – Scaler Topics

Learn about How to Find Square Root in Java ? As a programmer, I constantly find it fascinating to write programs for numerical operations I used to do by hand back in school. Square and square root of a number is one of the principles which are used in many real-life applications arsenic well as programming concepts, for exemplar prime factors of a phone number, binary exponentiation etc. If that doesn ’ t excite you, they are a hot subject in interviews angstrom well 🙂

Let ’ s start by defining what a square and straight root actually means .

What is Square?

Simply put, the square of a number is the number multiplied by itself. In early words, if we multiply a numeral by itself, the result is the square of that number. Square of X = X * X For exercise, if X = 3 Square of 3 = 3 * 3 = 9

What is Square Root?

Square root is precisely the opposition of the square of a number. The straight settle of a count adam is the act that when multiplied by itself equals X. Square root of X = √X where √ is the symbol for feather root For model, if X = 9 Square root of 9 = √9 = 3 The square solution of x can besides be represented by X 1/2. This is authoritative, keep reading to find out why 🙂 In a nutshell, if the square of a number X is S, then X is the straight root of S. It ’ south alright, try to breathe that in for a moment 🙂 now that we know what we mean by the square and feather root of a total, let ’ s see how we can write a broadcast for it in Java .

How to square a number in Java?

There are a match of ways to find the square of a issue in Java. Let ’ s attend at them one by one. 1. Multiplying the number by itself It ’ second angstrom simple as it sounds. Just multiply the total by itself and you ’ rhenium well to go .

 int adam = 3 ; int S = X * X ; System.out.println ( `` The feather of `` + X + `` is `` + S ) ; // The square of 3 is 9

2. Using Math.pow Method Math is a utility class in Java that has many utilitarian mathematical functions, one of which is prisoner of war which allows you to calculate the exponentiation or power of a number .

Syntax:

 public  static  double  prisoner of war ( double free-base, double exponent ) ;

Parameters : base, exponent Returns : base to the power of exponent Both parameters accept double as the stimulation. The render type is besides a double value. If you pass an integer or any primitive numeral value to either basal or exponent, it will mechanically be cast to double in Java. therefore, if base = 10, exponent = 2 Math.pow ( 10, 2 ) = 100.0 so, in ordain to find the squarely of a number, we can pass the root as the act and the advocate as 2. here ’ s a snip that shows the use of Math.pow .

 int x = 3 ; doubly S = Math.pow ( X, 2 ) ; System.out.println ( `` The square of `` + X + `` is `` + S ) ; // The hearty of 3 is 9.0

How to find the square root of a number in Java?

Calculating the feather of a phone number was pretty aboveboard right. But calculating the square settle of a numeral is a spot more concern. Let ’ s see how we can do that. 1. Using Math.sqrt This is the friendly sibling of the Math.pow function we saw in the previous segment. Like the way it reads, it finds the square root of a total .

Syntax

 public  static  double over  sqrt ( double number ) ;

Parameters : issue Returns : The square rout of number The number parameter accepts double as input and the return type is besides a double measure. so, if number = 9 Math.sqrt ( 9 ) = 3.0 here ’ s a snip that demonstrates the use of Math.sqrt

 int adam = 9 ; double R = Math.sqrt ( X ) ; System.out.println ( `` The square etymon of `` + X + `` is `` + R ) ;
 // The square settle of 9 is 3.0

2. Using Math.pow You : Say, what ? Math.pow can be used to find the square root of a number as well ? Rahul : That ’ randomness right. merely like we used Math.pow to find the square of a number, we can use it to find the square ancestor of a number angstrom well. You : But how ? Rahul : Remember the way the squarely root of a number is represented : X1/2 ; it means the square root of a number is X to the power of ½. so if we pass the prize of the exponent in Math.pow as 0.5 which is ½, the resulting number will be the square ancestor. Let ’ s try that in the below snip .

 int ten = 9 ; double over R = Math.pow ( X, 0.5 ) ; System.out.println ( `` The square root of `` + X + `` is `` + R ) ; // The square beginning of 9 is 3.0

neat right ? The same routine is used to calculate two different operations. 3. Without using any inbuilt function now that we have looked at a few ways to find the square solution of a number using built-in functions in Java, let ’ s look at a direction without using any such built-in routine. Let me propose an algorithm foremost and then we ’ ll break it down one step at a time .

  • Start from i = 1, if i * i == n, then i is the square root of n as n is a perfect square.
  • if i * i > n, it means the square root must lie between (i-1, i), let’s call them (low, high)
  • Apply binary search in the range (low, high). Find mid of (low, high):
  1. if mid * mid == n, it means we assume that mid is the square root of n
  2. if mid * mid > n, it means that the assumption we made is incorrect. This Implies, the square root of n must be less than mid, since any value higher than mid can never satisfy the condition mid * mid == n. Hence, we will try to find the square root in the left side of mid by repeating step 3 for (low, mid)
  3. else mid * mid < n, means that the assumption we made is incorrect. This Implies, the square root of n must be greater than mid, since any value less than mid can never satisfy the condition mid * mid == n. Hence, we will try to find the square root in the right side of mid by repeating repeat step 3 for (mid, high)

What is Perfect Square?

A arrant square is an integer which is the square of an integer. For case, X = 9 = 3 * 3 = 3^2 here, 9 is a perfective square because 9 is the square of 3. On the other bridge player, 10 is not a perfective feather because it can not be represented as the square of an integer. then, if we want to calculate the square root of X which is a perfect feather, we need to find a act which when multiplied with itself equals X. You : That ’ randomness great, but what about numbers that are not a perfect square ? Rahul : great question 🙂 Numbers that are not a perfect square will have a real number ( decimal fraction ) as their square etymon. so, we need a direction to find the nearest approximate respect for the feather root ( up to 6 decimal places for nowadays ). It ’ ll be something like : R.abcdef Let ’ s say we need to find the square settle of 13, which is not a perfect square. Let ’ s find R foremost. watch that the maximal perfective feather less than 13 is 9 whose square ancestor is 3. Since the square of 4 is 16, which is greater than 13, the square root of 13 has to be less than 4. So we can be certain that the square root of 13 as well will be something like 3.abcdef. now that we have the value of R ( 3 ), let ’ s determine abcdef. In order to find the decimal fraction places, we can use binary star Search. We know that the square settle will lie between ( 3, 4 ). We apply binary search in this range, to find the value whose square is equal to 13 ( up to 6 decimal places ). here is a sample distribution walkthrough of how we are applying binary search .

 low = 3, high = 4, mid = 3.5, mid * mid = 12.25  < 13 low = 3.5, eminent = 4, mid = 3.75, mid * mid = 14.0625 > 13 humble = 3.5, eminent = 3.75, mid = 3.625, mid * mid = 13.140625 > 13 low = 3.5, high = 3.625, mid = 3.5625, mid * mid = 12.69140625 < 13.
 .
 .
 low = 3.605550, high = 3.605551, mid = 3.605551, mid * mid = 12.9999997 == 13 ( up to 6 decimal places )

Hence, the square root of 13 = 3.605551 here is a snip of this algorithm

 private  static  double  sqrt ( double adam )  { for ( int one = 1 ; one < ten ; ++i ) { int phosphorus = i * iodine ; if ( phosphorus == X ) { // perfective square hark back i ;        }
 if ( p > X ) { // found left part of decimal return sqrt ( X, i - 1, iodine ) ;        }
     }
 render Double.NaN ;}
 
 
 private  electrostatic  double  sqrt ( double ten, double depleted, double high )  { double mid = ( low + gamey ) / 2 ; double phosphorus = mid * mid ; // uncomment below wrinkle to see how we reach the final answer // System.out.println ( low + `` `` + high + `` `` + mid + `` `` + phosphorus ) ;
 if ( p == X || ( Math.abs ( X - phosphorus ) < PRECISION ) ) { return mid ;    }
 if ( p < X ) { hark back sqrt ( X, mid, high ) ;    }
 refund sqrt ( X, low, mid ) ; }
 

Conclusion

today we learned how a simpleton problem of finding the square and square ancestor of a phone number can be solved in different ways in Java. Thanks for reading. May the code be with you 🙂

Exercise

What will happen if a negative numeral is passed as the exponent parameter to the Math.pow serve ? What will happen if a negative number is passed to the Math.sqrt function ? Can you think of improving the time complexity of the binary search algorithm we used above to find the square root of a number ? hint : Can we apply binary search somewhere else vitamin a well ? 😛

source : https://thefartiste.com
Category : Tech

About admin

I am the owner of the website thefartiste.com, my purpose is to bring all the most useful information to users.

Check Also

articlewriting1

Manage participants in a zoom meeting webinar

Call the people who attend the meet as follows Alternate host host Who scheduled the …

Leave a Reply

Your email address will not be published.