Skip to content

Commit

Permalink
Update Log2
Browse files Browse the repository at this point in the history
  • Loading branch information
itsubaki committed Apr 19, 2024
1 parent 993db57 commit 2704db9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 4 additions & 4 deletions math/number/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ func Log2(N int) (int, error) {
return 0, nil
}

if N%2 != 0 {
return -1, fmt.Errorf("N must be a power of 2. N=%v", N)
}

var n int = 1
for {
if N%2 != 0 {
return -1, fmt.Errorf("N must be a power of 2")
}

if N/2 == 1 {
break
}
Expand Down
6 changes: 5 additions & 1 deletion math/number/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ import (

func ExampleLog2() {
fmt.Println(number.Log2(8))
fmt.Println(number.Log2(32))
fmt.Println(number.Log2(15))
fmt.Println(number.Log2(20))

// Output:
// 3 <nil>
// -1 N must be a power of 2. N=15
// 5 <nil>
// -1 N must be a power of 2
// -1 N must be a power of 2
}

func TestLog2(t *testing.T) {
Expand Down

0 comments on commit 2704db9

Please sign in to comment.