Skip to content

Commit 75aac3b

Browse files
authored
Merge pull request #1889 from saip7795/sp/power-x-n
Create: 0050-power-x-n.rb
2 parents 8da7688 + 0aff936 commit 75aac3b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Diff for: ruby/0050-power-x-n.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
def my_pow(x, n)
3+
solution = multiply(x, n.abs)
4+
5+
if n>=0
6+
return solution
7+
else
8+
return (1/solution)
9+
10+
end
11+
end
12+
13+
14+
def multiply(x,n)
15+
return 0 if x==0
16+
return 1 if n==0
17+
result = multiply((x*x),n/2)
18+
if (n%2 ==1)
19+
return (x * result)
20+
else
21+
return result
22+
end
23+
end

0 commit comments

Comments
 (0)