-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrtComparison.java
More file actions
79 lines (65 loc) · 2.65 KB
/
Copy pathSqrtComparison.java
File metadata and controls
79 lines (65 loc) · 2.65 KB
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
public class SqrtComparison {
/**
* Calculating the integer square root of a number using the Babylonian method.
* Time Complexity for Babylonian Method: O(log(log n))*
**/
public static long sqrtBabylonian(int n) {
if (n < 0) throw new IllegalArgumentException("Cannot calculate square root of a negative number.");
if (n == 0) return 0;
// initial guess (n lete hai generally )
long x = n;
while (true) {
long next_x = (x + n / x) / 2;
if (next_x >= x) {
return x;
}
x = next_x;
}
}
/**
* Calculating integer square root of a number using Binary Search.
* TC: O(log n) ham div use karenge instead of multiplication
**/
public static long sqrtBinarySearch(int n) {
if (n < 0) throw new IllegalArgumentException("Cannot calculate square root of a negative number.");
if (n < 2) return n;
long low = 1;
long high = n;
long result = 0;
while (low <= high) {
long mid = low + (high - low) / 2;
if (mid <= n / mid) {
result = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return result;
}
public static void main(String[] args) {
int number = Integer.MAX_VALUE; //bade value ke liye
System.out.println("Finding integer square root of: " + number);
System.out.println("------------------------------------------");
// --- Babylonian Method ---
long startTimeB = System.nanoTime();
long resultB = sqrtBabylonian(number);
long endTimeB = System.nanoTime();
long durationB = endTimeB - startTimeB;
System.out.println("Babylonian Method ");
System.out.println("Result: " + resultB);
System.out.println("Time taken: " + durationB + " nanoseconds.");
System.out.println();
// --- Binary Search Method ---
long startTimeBS = System.nanoTime();
long resultBS = sqrtBinarySearch(number);
long endTimeBS = System.nanoTime();
long durationBS = endTimeBS - startTimeBS;
System.out.println("Binary Search Method ");
System.out.println("Result: " + resultBS);
System.out.println("Time taken: " + durationBS + " nanoseconds.");
System.out.println("------------------------------------------");
double factor = (double) durationBS / durationB;
System.out.printf("Conclusion: The Babylonian method was %.2f times faster for this input.%n", factor);
}
}