Leetcode 202. Happy number

Source: Internet
Author: User

Write an algorithm to determine if a number is "happy".

A happy number is a number defined by the following process:starting with any positive integer and replace the number by the Sum of the squares of its digits, and repeat the process until the number equals 1 (where it would stay), or it loops Endl essly in a cycle which does not include 1. Those numbers for which this process ends in 1 is happy numbers.

Example: is a happy number

    • 12 + 92 = 82
    • 82 + 22 = 68
    • 62 + 82 = 100
    • 12 + 02 + 02 = 1

The Natural solution is:

Class solution (Object):    def ishappy (self, N): "" "        : Type n:int        : Rtype:bool        " ""                def sum2 (num) :            ans = 0 while num! =            0:                ans + = (num) **2                num = Num/10            return ans                m = set () while        n No T in M:            if n = = 1:                return True            m.add (n)            n = sum2 (n)        return False

There are also ways to use the loop linked list, a fast and fast hand race, until you catch up:

Class solution (Object):    def ishappy (self, N): "" "        : Type n:int        : Rtype:bool        " ""                def sum2 (num) :            ans = 0 while num! =            0:                ans + = (num) **2                num = Num/10            return ans                slow = n        fast = sum2 (SUM2 (n))        While slow! = Fast:            slow = sum2 (slow)            fast = sum2 (sum2 (FAST))                        return slow = = 1

Java code is actually very elegant:

int digitsquaresum (int n) {    int sum = 0, tmp;    while (n) {        tmp = n;        sum + = tmp * TMP;        n/=;    }    return sum;} BOOL Ishappy (int n) {    int slow, fast;    slow = fast = N;    Do {        slow = digitsquaresum (slow);        Fast = digitsquaresum (fast);        Fast = digitsquaresum (fast);    } while (slow! = fast);    if (slow = = 1) return 1;    else return 0;}

Mathematical Solutions:

Class Solution {Public:int Loop[8] = {4,16,37,58,89,145,42,20};bool Inloop (int n) {for    (auto x:loop) {        if (x = = N) r Eturn true;    }    return false;} BOOL Ishappy (int n) {    if (n = = 1) return true;    if (Inloop (n)) return false;    int next = 0;    while (n) {        next + = (n%10) * (N%10);        n/=;    }    Return Ishappy (next);};

Proof:1.loop number is less than 162.Assume f (x) is the sum of the squares of X ' s digits. Let's say 0 < x <= 9,999,999,999 which is larger than the range of an int. f (x) <= 9^2 * 10 = 810. So no mater how big X was, after one step of f (x), it would be less than 810.The most large f (x) value (x < 810) is F (799 ) = 211. And do this several times:f (211) < F (199) = 163. F (163) < f (99) = 162. So no mater which x choose after several times of F (x), it finally fall in the range of [1,162] and can never get out.2 . I check every unhappy number in range of [1,162], they all fall in loop {4,16,37,58,89,145,42,20}, which means every Unha Ppy number fall in this loop.

In fact, through the enumeration should know that the above <=810 range of numbers will fall in a specific range, by shrinking the scope to find the law. =

Using fact all numbers in [2, 6] is not happy (and all is not happy numbers end on a cycle that hits this interval):

  bool Ishappy (int n) {while (n>6" {int next =  0; while (n) {next+= (n%10) * (n%10); n/=10;} n = Next;} return n==1;} 
/code>
Class solution (Object):    def ishappy (self, N): "" "        : Type n:int        : Rtype:bool        " ""                def sum2 (num) :            ans = 0 while num! =            0:                ans + = (num) **2                num = Num/10            return ans while                n > 6:            n = SUM2 (n)        return n = = 1

 

Leetcode 202. Happy number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.