Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

202. Happy Number

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
2
3
4
5
6
7
Input: n = 19
Output: true
Explanation:
1ˆ2 + 9ˆ2 = 82
8ˆ2 + 2ˆ2 = 68
6ˆ2 + 8ˆ2 = 100
1ˆ2 + 0ˆ2 + 02 = 1

Example 2:

1
2
Input: n = 2
Output: false

題目說快樂數是把輸入各分位平方總和到只有個位,當最後這個數是1就快樂(不要問我為什麼這樣很快樂,我也不知道啊啊啊?

主要就讓他一直算sum([int(i)**2 for i in str(n)])


如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels