Last active
April 30, 2020 17:23
-
-
Save SumanSudhir/d31fd52f6579d8242da240f60e61132c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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