Happy Number

Try to solve the Happy Number problem.

Statement#

Write an algorithm to determine if a number nn is a happy number.

We use the following process to check if a given number is a happy number:

  • Starting with the given number nn, replace the number with the sum of the squares of its digits.
  • Repeat the process until:
    • The number equals 11, which will depict that the given number nn is a happy number.
    • The number enters a cycle, which will depict that the given number nn is not a happy number.

Return TRUE if nn is a happy number, and FALSE if not.

Constraints

  • 11 \leq nn 2311\leq 2^{31} - 1

Examples#

Created with Fabric.js 3.6.6 3 n 23 13 ? We added the square ofthe two digits, and got thevalue of n for the next iteration. 2 2 2 Inputs Explanation Output Sample example 1

1 of 6

Created with Fabric.js 3.6.6 1 3 n 23 ? We added the square ofthe two digits, and got thevalue of n for the next iteration. 2 2 Inputs Explanation Output 10 Sample example 1

2 of 6

Created with Fabric.js 3.6.6 0 1 n 23 2 2 Inputs Explanation Output TRUE 1 Sample example 1 We added the square ofthe two digits, and got thevalue 1. Hence, this is a happy number.

3 of 6

Created with Fabric.js 3.6.6 2 2 Inputs Explanation Output Sample example 2 n 2 ? 4 We took the square of thedigit, which gave us 4. Thisbecomes the value for thenext iteration.

4 of 6

Created with Fabric.js 3.6.6 4 2 Inputs Explanation Output Sample example 2 n 2 ? 16 We took the square of thedigit, which gave us 16. Thisbecomes the value for thenext iteration.

5 of 6

Created with Fabric.js 3.6.6 Inputs Explanation Output Sample example 2 n 2 FALSE The next elements in the sequencewill be as follows: 16 -> 37 -> 58 -> 89 -> 145 ->42 -> 20 -> 4As we've encountered 4 before, itindicates that there exists a cycle,and 2 is not a happy number.

6 of 6

Test your understanding of the problem#

Let’s take a moment to make sure we have correctly understood the problem. The quiz below helps us to check that we are solving precisely the right problem:

Happy Number

1

(True or False) 28 is a happy number.

A)

True

B)

False

Question 1 of 20 attempted

Figure it out#

We have a game for you to play: re-arrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Drag and drop the cards to re-arrange them in the correct sequence.

Set up two pointers, slow_pointer points to the input number, and fast_pointer points to the sum of squared digits of the input number.

Start a loop until fast_pointer reaches 1 or both pointers meet, indicating a cycle.

Update the slow_pointer by adding the squares of its digits and fast_pointer by adding the squares of its digits twice.

If fast_pointer equals 1, return TRUE, indicating the number is happy.

Otherwise, if the loop exits without finding a happy number, return FALSE, indicating the presence of a cycle and that the number is not happy.


Try it yourself#

Implement your solution in main.py in the following coding playground. We have provided a useful code template that you may build on to solve this problem.

Python
main.py
sum_of_squared_digits.py
Input #1
Happy Number

Fast and Slow Pointers: Introduction

Solution: Happy Number