```
bool isHappy(int n) {
//if(n==1) return true;
int r=0;
int digit=n;
int tmp=n;
while(1)
{
while(tmp!=0)
{
digit=tmp%10;
r+=digit*digit;
tmp=tmp/10;
}
if(r==4) return false;
else if(r==1) return true;
else
{
tmp=r;
r=0;
}
}
}
```

each !isHappy num will encounter 4 in the loop.