-
Notifications
You must be signed in to change notification settings - Fork 0
/
PerfectCube.py
42 lines (39 loc) · 888 Bytes
/
PerfectCube.py
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
def PerfectCube(left,right,x):
'''
Fast O(logn) algorithm to find check if an element is perfect cube or not
'''
if (right<left):
return 'No'
else:
mid = (left+right)//2
if (x==mid**3):
return 'Yes'
elif (x<mid**3):
return PerfectCube(left,mid-1,x)
else:
return PerfectCube(mid+1,right,x)
n = 216
print(PerfectCube(0,n,n))
'''
Alernative 1
def PerfectCube(Array,left,right,x):
if (right<left):
return 'No'
else:
mid = (left+right)//2
if (x**(1/3)==Array[mid]):
return 'Yes'
elif (x**(1/3)<Array[mid]):
return PerfectCube(Array,left,mid-1,x)
else:
return PerfectCube(Array,mid+1,right,x)
A = [i for i in range(0,10000)]
n =
print(PerfectCube(A,0,len(A)-1,n))
'''
'''
Alternative Fast Algorithm 2
def is_perfect_cube(number):
number = abs(number)
return round(number**(1 / 3))**3==number
'''