-
Notifications
You must be signed in to change notification settings - Fork 1
/
HappyNumber.cpp
41 lines (35 loc) · 872 Bytes
/
HappyNumber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Happy number
// Leetcode : https://leetcode.com/explore/interview/card/top-interview-questions-medium/113/math/815/
#include <unordered_set>
#include <iostream>
int sumOfSquares(int number, int base = 10)
{
int sum = 0;
while (number > 0) {
int digit = number % base;
sum = digit * digit + sum;
number = number / base;
}
return sum;
}
bool isHappyNumber(int number)
{
std::unordered_set<int> seen;
while (number > 1 && (seen.find(number) == seen.end())) {
seen.insert(number);
number = sumOfSquares(number);
}
return (number == 1);
}
void test(const int number)
{
std::cout << number << " is happy number : " << std::boolalpha << isHappyNumber(number) << "\n";
std::cout << "===============================\n";
}
int main()
{
test(19);
test(2);
test(998);
return 0;
}