Skip to content

Commit 09be7f5

Browse files
committed
Add number of 0's in a factorial and update gitignore
1 parent 23a112e commit 09be7f5

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

.gitignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
# Compiled class file
22
*.class
33

4-
# a.out
4+
# a.out/gcov
55
*a.out
6+
*.cpp.gcov
7+
*.gcda
8+
*.gcno
9+
*.c.gcov
610

711
# Log file
812
*.log

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ Princeton Algorithms [Part 1](https://www.coursera.org/learn/algorithms-part1/)
197197
* Iterative [C++](problems/math/PascalTriangle.cpp)
198198
* Happy number [Wiki](https://en.wikipedia.org/wiki/Happy_number) [C++](problems/math/HappyNumber.cpp)
199199
* Excel Sheet Column Number [C++](problems/math/SheetColumnNumber.cpp)
200+
* Number of zeros in facorial [C++](problems/math/FactorialTrailingZeros.cpp)
200201

201202
## Permutation
202203
* Permutation of objects [Java](problems/queue/Permutation.java)
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// number of trailing zeroes in n!
2+
// Leetcode https://leetcode.com/explore/interview/card/top-interview-questions-medium/113/math/816/
3+
4+
#include <iostream>
5+
#include <climits>
6+
7+
int trailingZeroes(const int n)
8+
{
9+
int result = 0;
10+
for (long i = 5; i <= n; i = i * 5) {
11+
result = result + n / i;
12+
}
13+
return result;
14+
}
15+
16+
void test(const int n)
17+
{
18+
std::cout << "Number of trailing zeros in " << n << "! : " << trailingZeroes(n) << "\n";
19+
}
20+
21+
int main()
22+
{
23+
test(INT_MIN);
24+
test(-1);
25+
test(0);
26+
test(1);
27+
test(5);
28+
test(10);
29+
test(125);
30+
test(200);
31+
test(INT_MAX);
32+
return 0;
33+
}

0 commit comments

Comments
 (0)