Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Happy Number (medium)

Problem Statement

Any number will be called a happy number if, after repeatedly replacing it with a number equal to the sum of the square of all of its digits, leads us to the number 1. All other (not-happy) numbers will never reach 1. Instead, they will be stuck in a cycle of numbers that does not include 1.

Given a positive number n, return true if it is a happy number otherwise return false.

Example 1:

Input: 23
Output: true (23 is a happy number)
Explanations: Here are the steps to find out that 23 is a happy number:

2^2 +3^2 = 4 + 9 = 13

.....

.....

.....

Like the course? Get enrolled and start learning!