File tree 3 files changed +43
-0
lines changed
src/algorithm_practice/Number_Algorithms/harshadNumbers
3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments