- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcubeRoot.py
More file actions
Latest commit
32 lines (28 loc) · 584 Bytes
/
Copy pathcubeRoot.py
File metadata and controls
32 lines (28 loc) · 584 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 18 21:10:14 2017
@author: root
"""
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Oct 18 16:57:58 2017
@author: root
"""
x=27
epsilon=0.01
numGuesses=0
low=0.0
high=max(1.0, x)
ans= (high+low)/2.0
whileabs(ans**3-x) >=epsilon:
print ('low =', low, 'high =', high, 'ans =', ans)
numGuesses+=1
ifans**3<x:
low=ans
else:
high=ans
ans= (high+low)/2.0
print ('numGuesses =', numGuesses)
print (ans, 'is close to square root of', x)