Write an algorithm to determine if a number n is happy.
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.
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.
Return true if n is a happy number, and false if not.
Example 1:
1 | Input: n = 19 |
Example 2:
1 | Input: n = 2 |
題目說快樂數是把輸入各分位平方總和到只有個位,當最後這個數是1就快樂(不要問我為什麼這樣很快樂,我也不知道啊啊啊?
主要就讓他一直算sum([int(i)**2 for i in str(n)])
如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)