We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 48af54f commit 46c62dcCopy full SHA for 46c62dc
069_sqrt/Makefile
@@ -0,0 +1,2 @@
1
+all:
2
+ gcc -O2 -o test sqrt.c
069_sqrt/sqrt.c
@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <stdlib.h>
3
+
4
+static int mySqrt(int x)
5
+{
6
+ if (x == 0) {
7
+ return 0;
8
+ }
9
10
+ unsigned int left = 1;
11
+ unsigned int right = (unsigned int) x;
12
+ for (; ;) {
13
+ unsigned int mid = left + (right - left) / 2;
14
+ if (mid > x/mid) {
15
+ right = mid;
16
+ } else {
17
+ if (mid + 1 > x/(mid + 1)) {
18
+ return mid;
19
20
+ left = mid;
21
22
23
24
+}
25
26
+int main(int argc, char **argv)
27
28
+ if (argc != 2) {
29
+ fprintf(stderr, "Usage: ./test n\n");
30
+ exit(-1);
31
32
33
+ printf("%d\n", mySqrt(atoi(argv[1])));
34
35
0 commit comments