-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05 Fast Integer Cube and Square Root.cpp
More file actions
65 lines (50 loc) · 1012 Bytes
/
05 Fast Integer Cube and Square Root.cpp
File metadata and controls
65 lines (50 loc) · 1012 Bytes
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
/**
Code : mochow13
**/
unsigned int fast_sqrt(unsigned int n){
unsigned int c, g;
c = g = 0x8000;
for (; ;){
if ((g * g) > n) g ^= c;
c >>= 1;
if (!c) return g;
g |= c;
}
}
int fast_cbrt(int n){
int x, r = 30, res = 0;
for (; r >= 0; r -= 3){
res <<= 1;
x = (3 * res * (res + 1)) + 1;
if ((n >> r) >= x){
res++;
n -= (x << r);
}
}
return res;
}
unsigned long long fast_sqrt(unsigned long long n){
unsigned long long c, g;
c = g = 0x80000000;
for (; ;){
if ((g * g) > n) g ^= c;
c >>= 1;
if (!c) return g;
g |= c;
}
}
unsigned long long fast_cbrt(unsigned long long n){
int r = 63;
unsigned long long x, res = 0;
for (; r >= 0; r -= 3){
res <<= 1;
x = (res * (res + 1) * 3) + 1;
if ((n >> r) >= x){
res++;
n -= (x << r);
}
}
return res;
}
int main(){
}