Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b652e5f

Browse files
author
openset
committed
Add: Power of Three
1 parent ee5486d commit b652e5f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
package power_of_three
2+
3+
func isPowerOfThree(n int) bool {
4+
return n > 0 && 1162261467%n == 0
5+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,39 @@
11
package power_of_three
2+
3+
import "testing"
4+
5+
type caseType struct {
6+
input int
7+
expected bool
8+
}
9+
10+
func TestIsPowerOfThree(t *testing.T) {
11+
tests := [...]caseType{
12+
{
13+
input: 27,
14+
expected: true,
15+
},
16+
{
17+
input: 0,
18+
expected: false,
19+
},
20+
{
21+
input: 1,
22+
expected: true,
23+
},
24+
{
25+
input: 9,
26+
expected: true,
27+
},
28+
{
29+
input: 45,
30+
expected: false,
31+
},
32+
}
33+
for _, tc := range tests {
34+
output := isPowerOfThree(tc.input)
35+
if output != tc.expected {
36+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
37+
}
38+
}
39+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /