Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1889 from saip7795/sp/power-x-n
Browse files Browse the repository at this point in the history
 Create: 0050-power-x-n.rb
  • Loading branch information
tahsintunan authored Jan 8, 2023
2 parents 8da7688 + 0aff936 commit 75aac3b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ruby/0050-power-x-n.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

def my_pow(x, n)
solution = multiply(x, n.abs)

if n>=0
return solution
else
return (1/solution)

end
end


def multiply(x,n)
return 0 if x==0
return 1 if n==0
result = multiply((x*x),n/2)
if (n%2 ==1)
return (x * result)
else
return result
end
end

0 comments on commit 75aac3b

Please sign in to comment.