Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int sumDigitSquare(int n)
- {
- int ans = 0;
- while (n) {
- int digit = n % 10;
- ans += digit * digit;
- n /= 10;
- }
- return ans;
- }
- bool isHappy(int n)
- {
- while (1) {
- if (n == 1)
- return true;
- // Replace n with sum of squares
- // of digits
- n = sumDigitSquare(n);
- // Number is not Happy if we reach 4
- if (n == 4)
- return false;
- }
- return false;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement