Loading...
Loading...
Loading...
Start with a positive integer and replace it by the sum of the squares of its digits. Repeat. If you reach 1 the number is happy. If the numbers go round in a loop that never contains 1, it is not. Print YES for a happy number and NO otherwise. Use a slow and a fast pointer, not a set.
Example 1
Input
19
Output
YES
Example 2
Input
2
Output
NO
Try the problem first. Open one hint at a time.
The next number is a function of the current one, so the sequence is a chain without a list.
Move slow one step and fast two steps. Stop when fast is 1 or when slow and fast meet.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor