Happy Number - aloalgo

Happy Number

Easy

You are given a positive integer n.

Your task is to determine whether n is a happy number. A happy number is defined by the following process: starting with the given number, replace it with the sum of the squares of its digits, and repeat until the number either equals 1 or enters an endless cycle.

Note that:

  • A number is happy if this process eventually reaches 1.
  • A number is not happy if this process enters a cycle that does not include 1.

Return true if n is a happy number, and false otherwise.

Example 1

Input
19
Output
True
Explanation:

1² + 9² = 82 → 8² + 2² = 68 → 6² + 8² = 100 → 1² + 0² + 0² = 1. The process reaches 1, so 19 is a happy number.

Example 2

Input
2
Output
False
Explanation:

2 → 4 → 16 → 37 → 58 → 89 → 145 → 42 → 20 → 4 → … The process enters a cycle and never reaches 1, so 2 is not a happy number.

Example 3

Input
1
Output
True
Explanation:

1² = 1. The number is already 1, so it is a happy number.

Loading...
Input
19
Output
True

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!