Skip to content

Instantly share code, notes, and snippets.

@SumanSudhir
Last active April 30, 2020 17:23
Show Gist options
  • Save SumanSudhir/d31fd52f6579d8242da240f60e61132c to your computer and use it in GitHub Desktop.
Save SumanSudhir/d31fd52f6579d8242da240f60e61132c to your computer and use it in GitHub Desktop.
// Every number > 9 on sum of squared expansion will reduce to number < 9
// In [0-9], there are only two cases when number < 9 and it is happy number i.e 1 and 7
class Solution {
public:;
int sumSquare(int n) {
int sum = 0;
while(n !=0){
sum += (n%10) * (n%10);
n = n/10;
}
return sum;
}
bool isHappy(int n) {
int sum = sumSquare(n);
while(true){
if(sum == 1 || sum == 7) return true;
if(sum/10 == 0) return false;
sum = sumSquare(sum);
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment