Skip to content

Commit 6f9d72b

Browse files
committed
algo(harshadNumbers): add algo + light docs + problem statement picture
1 parent 307f830 commit 6f9d72b

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Harshad Numbers
2+
3+
Source:
4+
5+
- ACMICPC Regionals 2018 Hosted @ University of Cincinnati
6+
7+
![harshad numbers problem statement picture](./harshadNumbers.jpg)
8+
9+
This is a relatively straight-forward brute-force algorithm, which
10+
practically does not need explained at all.
11+
12+
# Complexity analysis
13+
14+
- Time complexity: O(log(n)\*k), where n = the nth harshad number, and k is the
15+
gap between the input number and the next harshad number (I don't know the exact
16+
relationship between/formula of the harshad numbers so I am just generalizing the
17+
complexity to this text)
18+
- Space complexity: O(1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
3+
// Source: ACM ICPC 2018
4+
5+
bool harshad(long in) {
6+
long saveIn = in;
7+
int sum = 0;
8+
while (in) {
9+
sum += in % 10;
10+
in /= 10;
11+
}
12+
13+
return (saveIn % sum == 0);
14+
}
15+
16+
int main() {
17+
long input;
18+
std::cin >> input;
19+
20+
while (!harshad(input))
21+
input++;
22+
23+
std::cout << input << std::endl;
24+
return 0;
25+
}
Loading

0 commit comments

Comments
 (0)