-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path50. Pow(x, n).rb
More file actions
64 lines (54 loc) · 929 Bytes
/
50. Pow(x, n).rb
File metadata and controls
64 lines (54 loc) · 929 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
=begin
def my_pow(x, n)
return 1 if n==0
if n>0
return x * my_pow(x,n-1)
else
return (1/x*my_pow(x,n+1)).round(10)
end
end
def my_pow(x, n)
return 1.0 if n==0
return x if n==1
if n < 0
x = 1/x
n = -n
end
return x**(n%2)*(my_pow(x, n/2)**2)
end
def my_pow(x, n)
return 1 if n == 0
n1 = n < 0 ? -n : n
x1, res = x, 1
while true
res *= x1 if n1.odd?
x1 *= x1
n1 >>= 1
break if n1 == 0
end
return (n < 0) ? 1 / res : res
end
=end
def my_pow(x,n)
return 1 if n==0
number=n.abs
ans=1.0
while number>0
if number.odd?
ans=ans*x
number=number-1
else
x=x*x
number=number/2
end
end
pp " #{ans} is an"
if n<0
ans = (1/ans).round(10)
end
return ans
end
x=5
n=-3
pp my_pow(x,n)
#result= 1/x**(n.abs)