Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. class Solution {
  2. public:
  3. int sumDigitSquare(int n)
  4. {
  5. int ans = 0;
  6. while (n) {
  7. int digit = n % 10;
  8. ans += digit * digit;
  9. n /= 10;
  10. }
  11. return ans;
  12. }
  13.  
  14. bool isHappy(int n)
  15. {
  16. while (1) {
  17.  
  18. if (n == 1)
  19. return true;
  20.  
  21. // Replace n with sum of squares
  22. // of digits
  23. n = sumDigitSquare(n);
  24.  
  25. // Number is not Happy if we reach 4
  26. if (n == 4)
  27. return false;
  28. }
  29.  
  30. return false;
  31. }
  32.  
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement