aboutsummaryrefslogtreecommitdiff
path: root/challenge-164/deadmarshal/cpp/ch-2.cpp
blob: cf0ef595f16fe91fac6775e05ca8ed375d5d109a (plain)
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
42
43
44
45
46
#include<iostream>
#include<unordered_map>
#include<cmath>

int sum_squares(int n)
{
  int sum{};
  while(n)
  {
    sum += (int)std::pow(n % 10, 2);
    n /= 10;
  }
  return sum;
}

bool is_happy(int n)
{
  std::unordered_map<int, int> map{};
  while(1)
  {
    map[n]++;
    n = sum_squares(n);
    if(n == 1) return true;
    if(map[n] != 0) return false;
  }
}

void happy_numbers()
{
  int i{}, count{};
  while(count < 8)
  {
    if(is_happy(i))
    {
      std::cout << i << ' ';
      count++;
    }
    i++;
  }
}

int main()
{
  happy_numbers();
  return 0;
}