-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrt(x).cpp
More file actions
75 lines (65 loc) · 1.45 KB
/
Sqrt(x).cpp
File metadata and controls
75 lines (65 loc) · 1.45 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
// Source : https://oj.leetcode.com/problems/sqrtx/
// Author : zheng yi xiong
// Date : 2015-01-31
/**********************************************************************************
*
* Implement int sqrt(int x).
* Compute and return the square root of x.
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
using namespace std;
class Solution {
public:
int sqrt(int x) {
//return sqrt1(x);
return sqrt2(x);
}
int sqrt2(int x)
{
if (x < 2)
{
return x;
}
int low = 1, hight = x / 2;
int mid = 0, mid2 = 0, last_mid = 1;
while (low <= hight)
{
mid = (low + hight) / 2;
mid2 = x / mid;
if (mid2 == mid)
{
return mid;
}
else if (mid2 < mid)
{
hight = mid - 1;
}
else
{
low = mid + 1;
last_mid = mid;
}
}
return last_mid;
}
//magical solution but not preciseness
float sqrt1(float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x; // get bits for floating VALUE
i = 0x5f375a86- (i>>1); // gives initial guess y0
x = *(float*)&i; // convert bits BACK to float
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
return 1/x;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Solution so;
int sqrtNum = so.sqrt(5);
return 0;
}