36 lines
551 B
Text
36 lines
551 B
Text
bool isHappy (int n)
|
|
{
|
|
ints cache;
|
|
|
|
while (n != 1)
|
|
{
|
|
int sum = 0;
|
|
|
|
if (cache.contains(n))
|
|
return false;
|
|
|
|
cache.add(n);
|
|
while (n != 0)
|
|
{
|
|
int digit = n % 10;
|
|
sum += (digit * digit);
|
|
n = (int)(n / 10);
|
|
}
|
|
n = sum;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void test ()
|
|
{
|
|
int num = 1;
|
|
ints happynums;
|
|
|
|
while (happynums.count() < 8)
|
|
{
|
|
if (isHappy(num))
|
|
happynums.add(num);
|
|
num++;
|
|
}
|
|
puts("First 8 happy numbers : " + str.newline + happynums);
|
|
}
|