-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy path69.c
72 lines (58 loc) · 1.35 KB
/
69.c
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
#include <stdio.h>
#include <assert.h>
/* Newton's method */
int mySqrt(int square) {
#define EPSILON 0.00000001f
double x = 1.0;
double f, d;
for (int i = 0; i < 32; i++) {
f = x * x - square;
if ((f > 0 && f < EPSILON) || (f < 0 && f > -EPSILON)) break;
d = 2 * x;
x = x - f / d;
}
return x;
}
/* Fast Newton's method
* https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Approximations_that_depend_on_the_floating_point_representation
*/
int mySqrt0(int x) {
float f = x;
int val_int = *(int *)&f;
int a = 2;
val_int = (1 << 29) + (val_int >> 1) - (1 << 22) + a;
f = *(float *)&val_int;
double d = f;
d = 0.5 * d + 0.5 * x / d;
d = 0.5 * d + 0.5 * x / d;
d = 0.5 * d + 0.5 * x / d;
return d;
}
/* Binary Search */
int mySqrt1(int x) {
if (x <= 1) {
return x;
}
int l, m, r;
l = 1;
r = x;
while (l < r) {
m = (l + r) / 2;
if (x / m < m)
r = m;
else
l = m + 1;
}
return r - 1;
}
int main() {
assert(mySqrt1(0) == 0);
assert(mySqrt1(1) == 1);
assert(mySqrt1(2) == 1);
assert(mySqrt1(3) == 1);
assert(mySqrt1(8) == 2);
assert(mySqrt1(2147395599) == 46339);
assert(mySqrt1(2147395600) == 46340);
printf("all tests passed!\n");
return 0;
}