๐Ÿ“ฆ gopikrishna000 / templates-latest

๐Ÿ“„ sqrtll_binary_search.cpp ยท 60 lines
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<snippet>
	<content><![CDATA[

// Function to find square root
// of given number upto given
// precision
long double sqrtll(int number, int precision = 20)
{
	int start = 0, end = number;
	int mid;

	// variable to store the answer
	long double ans = 0;

	// for computing integral part
	// of square root of number
	while (start <= end) {
		mid = (start + end) / 2;
		if (mid * mid == number) {
			ans = mid;
			break;
		}

		// incrementing start if integral
		// part lies on right side of the mid
		if (mid * mid < number) {
			start = mid + 1;
			ans = mid;
		}

		// decrementing end if integral part
		// lies on the left side of the mid
		else {
			end = mid - 1;
		}
	}

	// For computing the fractional part
	// of square root upto given precision
	long double increment = 0.5;
	for (int i = 0; i < precision; i++) {
		while (ans * ans <= number) {
			ans += increment;
		}

		// loop terminates when ans * ans > number
		ans = ans - increment;
		increment = increment / 2;
	}
	return ans;
}


]]></content>
	<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
	<tabTrigger>sqrtll</tabTrigger>
	<!-- Optional: Set a scope to limit where the snippet will trigger -->
	<scope>source.c++</scope>
</snippet>