Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pow(x, n) challenge #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ We thank everybody that help this project reach better places of our community:
</a>
</td>
<!-- . -->
<!-- Collaborator -->
<td align="center">
<a href="https://github.com/GustavoStingelin">
<img src="https://github.com/GustavoStingelin.png" width="100px;" alt="GustavoStingelin github avatar"/><br>
<sub>
<b>Gustavo</b>
</sub>
</a>
</td>
<!-- . -->
</tr>
</table>

Expand Down
32 changes: 32 additions & 0 deletions leetcode/50 - pow(x, n)/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Challenge Description

[50 - Pow(x, n)](https://leetcode.com/problems/powx-n/) | Difficulty: <span style="color:orange">Medium</span>

## Description:
---
Implement pow(x, n), which calculates x raised to the power n (i.e., x^n).

Example 1:
```
Input: x = 2.00000, n = 10
Output: 1024.00000
```

Example 2:
```
Input: x = 2.10000, n = 3
Output: 9.26100
```

Example 3:
```
Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2^-2 = 1/2^2 = 1/4 = 0.25
```

**Constraints:**

- 100.0 < x < 100.0
- 2^31 <= n <= 2^31-1
- 10^4 <= x^n <= 10^4
55 changes: 55 additions & 0 deletions leetcode/50 - pow(x, n)/pow(x, n).go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
"fmt"
)

func myPow(x float64, n int) float64 {
// if the exponent is zero, the result is one
if n == 0 {
return 1
}
// if the exponent is less than zero, the result is one divided by pow with the positive exponent
// called recursively
if n < 0 {
return 1 / myPow(x, -n)
}
// if the exponent is odd, we call the function recursively, but transforming the exponent into
// even (-1) and multiplying by the base
if n%2 == 1 {
return x * myPow(x, n-1)
}
// if the exponent is even, we can reduce the steps to checkout the result, multiplying the base
// by the base and dividing the expoent by two in the recursively call
return myPow(x*x, n/2)
}

func main() {

// definition of a slice (stack) of test cases and addition of cases
cases := []struct {
base float64
exponent int
expected float64
}{
{2, 10, 1024},
{2.1, 3, 9.261000},
{2, -2, 0.25},
{1, 2147483647, 1},
{10, 0, 1},
{10, 1, 10},
{-5, 1, -5},
}

// running the test for each case in the stack
for _, c := range cases {
pow := myPow(c.base, c.exponent)
// to compare a floating point number, we need to check the subtraction difference, which must
// be less than 0.00000000001
if diff := (pow - c.expected); diff > -0.0000000001 && diff < 0.0000000001 {
fmt.Printf("OK myPow(%f, %d): %f\n", c.base, c.exponent, pow)
} else {
fmt.Printf("Error should be %f but got %f\n", c.expected, pow)
}
}
}