{"id":159,"date":"2017-10-06T20:51:09","date_gmt":"2017-10-06T12:51:09","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=159"},"modified":"2018-11-27T16:51:52","modified_gmt":"2018-11-27T08:51:52","slug":"202-happy-number","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=159","title":{"rendered":"202. Happy Number"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>Write an algorithm to determine if a number is &#8220;happy&#8221;.<\/p>\n<p>A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.<\/p>\n<p><strong>Example:\u00a0<\/strong><\/p>\n<pre><strong>Input:<\/strong> 19\r\n<strong>Output:<\/strong> true\r\n<strong>Explanation: \r\n<\/strong>1<sup>2<\/sup> + 9<sup>2<\/sup> = 82\r\n8<sup>2<\/sup> + 2<sup>2<\/sup> = 68\r\n6<sup>2<\/sup> + 8<sup>2<\/sup> = 100\r\n1<sup>2<\/sup> + 0<sup>2<\/sup> + 0<sup>2<\/sup> = 1<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def isHappy(self, n):\r\n        &quot;&quot;&quot;\r\n        :type n: int\r\n        :rtype: bool\r\n        &quot;&quot;&quot;\r\n        temp_list= []\r\n        result = self._add(n)\r\n        while result not in temp_list:\r\n            temp_list.append(result)\r\n            result = self._add(result)\r\n            if result == 1:\r\n                return True\r\n        return False\r\n\r\n    def _add(self, num):\r\n        result = 0\r\n        for i in str(num):\r\n            result += int(i)**2\r\n        return result\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Write an algorithm to determine if a number is &#038;<a href=\"https:\/\/www.ccagml.com\/?p=159\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">202. Happy Number<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/159"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=159"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/159\/revisions"}],"predecessor-version":[{"id":160,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/159\/revisions\/160"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}