Happy Number
A "Happy Number" is defined as a number that after finite number of "steps" - where
we sum the square of each digit each time - the result is a 1
. Given a number n
,
determine whether it is a happy number.
As a challenge, complete this question under constant space.
Parameters
n
: The number to check.
Result
true
orfalse
, depending on whether this number is a happy number.
Examples
Example 1
Input: n = 19
Output: true
Explanation:
11^2 + 9^2 = 82 28^2 + 2^2 = 68 36^2 + 8^2 = 100 41^2 + 0^2 + 0^2 = 1
Example 2
Input: n = 2
Output: false
Explanation:
12^2 = 4 24^2 = 16 31^2 + 6^2 = 37 43^2 + 7^2 = 58 55^2 + 8^2 = 89 68^2 + 9^2 = 145 71^2 + 4^2 + 5^2 = 42 84^2 + 2^2 = 20 92^2 + 0^2 = 4 10...
Constraints
1 <= n < 2^31
Try it yourself
Solution
Title
Script
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum
has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
Contrary to popular belief, Lorem
Ipsum
is not simply random text.
1 >>> a = [1, 2, 3] 2 >>> a[-1] 3 3