Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Determining happy numbers using recursion JavaScript
A happy number is a number which eventually reaches 1 when replaced by the sum of the square of each digit. Whereas if during this process any number gets repeated, the cycle will run infinitely and such numbers are called unhappy numbers.
For example ? 13 is a happy number because,
1^2 + 3^2 = 10 and, 1^2 + 0^2 = 1
On the other hand, 36 is an unhappy number.
We are required to write a function that uses recursion to determine whether or not a number is a happy number.
Algorithm
The key to this function is that we will have to keep a record of the numbers that have already appeared. If the same number makes another appearance, we return false else if the squared digits add up to 1, we return true.
We will use an object to keep track of the already appeared number. We could've also used Set or Map, but a simple object will do it for us as well.
Recursive Helper Function
First, let's create a recursive function to calculate the sum of squares of digits:
const squareSumRecursively = (n, res = 0) => {
if(n){
return squareSumRecursively(Math.floor(n/10), res+Math.pow((n%10),2));
};
return res;
};
// Test the helper function
console.log(squareSumRecursively(13)); // Should give 1^2 + 3^2 = 10
console.log(squareSumRecursively(10)); // Should give 1^2 + 0^2 = 1
10 1
Main Happy Number Function
Now let's implement the main function that determines if a number is happy using recursion:
const squareSumRecursively = (n, res = 0) => {
if(n){
return squareSumRecursively(Math.floor(n/10), res+Math.pow((n%10),2));
};
return res;
};
const isHappy = (num, map = {}) => {
if(num !== 1){
if(map[num]){
return false;
}
map[num] = 1;
return isHappy(squareSumRecursively(num), map);
};
return true;
}
console.log(isHappy(36));
console.log(isHappy(13));
console.log(isHappy(23));
console.log(isHappy(7));
false true true true
How It Works
The algorithm works in two steps:
- squareSumRecursively(): This function recursively calculates the sum of squares of digits by extracting the last digit using modulo (%), squaring it, and recursively processing the remaining digits.
- isHappy(): This function uses recursion to check if a number leads to 1. It maintains a map to track visited numbers and detect cycles.
Step-by-Step Example
Let's trace through the execution for number 13:
Step 1: 13 ? 1² + 3² = 1 + 9 = 10 Step 2: 10 ? 1² + 0² = 1 + 0 = 1 Result: 1 (Happy number!)
For number 36:
Step 1: 36 ? 3² + 6² = 9 + 36 = 45 Step 2: 45 ? 4² + 5² = 16 + 25 = 41 Step 3: 41 ? 4² + 1² = 16 + 1 = 17 Step 4: 17 ? 1² + 7² = 1 + 49 = 50 Step 5: 50 ? 5² + 0² = 25 + 0 = 25 Step 6: 25 ? 2² + 5² = 4 + 25 = 29 ... eventually cycles back to a previous number Result: Unhappy number (cycle detected)
Conclusion
This recursive approach efficiently determines happy numbers by using a helper function to calculate digit squares and tracking visited numbers to detect cycles. The algorithm terminates when either 1 is reached (happy) or a cycle is detected (unhappy).
