-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquareRoot.java
85 lines (82 loc) · 1.78 KB
/
squareRoot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package algo;
import java.text.DecimalFormat;
/**
* This program finds the square root of a number
* without using any library function
* using binary search
* @author Azmain
*
*/
public class squareRoot {
public static void main(String[] args) {
sqrt(5);
sqrtWithPrecision(5,10);
}
/**
* finds the best possible square root of number
* @param number
*/
public static void sqrt(int number) {
float precision = 0.0001f;
float start = 0;
float end = number;
float middle = start;
float difference = (float) Math.abs((Math.pow(middle, 2)-number));
//System.out.println(difference);
while(difference > precision) {
middle = (start+end)/2;
if(Math.pow(middle, 2)> number) {
end = middle;
}
else {
start = middle;
}
difference = (float) Math.abs((Math.pow(middle, 2)-number));
}
System.out.println(middle);
}
/**
* finds the square root of a number upto a precision
* @param number
* @param precision
*/
public static void sqrtWithPrecision(int number,int precision) {
int start = 0;
int end = number;
double ans = 0.0;
int mid = 0;
/**
* calculates the integral part
*/
while(start <= end) {
mid = (start+end)/2;
if(mid*mid == number) {
ans = mid;
break;
}
if(mid*mid < number) {
start = mid+1;
ans = mid;
}
else {
end = mid-1;
}
}
/**
* calculates the fractional part
*/
double increment = 0.1;
for(int i = 0; i < precision; i++) {
while(ans*ans <= number) {
ans += increment;
}
ans -= increment;
increment /= 10;
}
// Float can print upto 6 decimal point precision
System.out.println((float)ans);
// Double can print upto 14 decimal point precision
DecimalFormat df2 = new DecimalFormat("#.0000000000");
System.out.println(df2.format(ans));
}
}