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 f87fc5f

Browse files
committed
Add: Factorial Trailing Zeroes
1 parent 6a5f7a3 commit f87fc5f

File tree

2 files changed

+53
-0
lines changed

2 files changed

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

0 commit comments

Comments
(0)

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