Reputation: 33
I wrote an algorithm that recursively calculates the sum of the square of the given number's digits, and if the sum ends up to be equal to 1, I would like it to return true.
I verified that I calculated correctly the sum, if the given number is 19, the next one is going to be 82, followed by 68, and finally 100 which should return true.
I checked whether the program goes inside the if(sum==1)
and it does.
If I print the sum before return false;
, it weirdly prints the resulting sum in descending order (1, 68, 82) before returning false.
class Solution {
public boolean isHappy(int n) {
int sum =0;
int digit =0;
while (n>0) {
digit =n%10;
sum=sum+digit*digit;
n=n/10;
System.out.println(sum);
}
if(sum==1){
return true;
}
else{
isHappy(sum);
}
return false;
}
}
What is wrong with my code? Why does it not stop after the sum is 1 and it has to return true?
Upvotes: 0
Views: 1521
Reputation: 106
isHappy(100) does in fact return true. You can try it
However, isHappy(82) and isHappy(68) will return false because you never use the value from the recursive call. You just compute it and return false
class Solution {
public boolean isHappy(int n) {
int sum =0;
int digit =0;
while (n>0) {
digit =n%10;
sum=sum+digit*digit;
n=n/10;
System.out.println(sum);
}
if(sum==1){
return true;
}
else{
isHappy(sum); -- the recursive call's value is unused
}
return false; -- you return false regardless of the computed value
}
}
the only change you have to make for it to work as expected is:
class Solution {
public boolean isHappy(int n) {
int sum =0;
int digit =0;
while (n>0) {
digit =n%10;
sum=sum+digit*digit;
n=n/10;
System.out.println(sum);
}
if(sum==1){
return true;
}
return isHappy(sum); -- Here
}
}
Upvotes: 0
Reputation: 462
You must return the value of your recursive call.
This will be your stack when you call isHappy(19)
:
- sum != 0. Call isHappy(82). return false
- sum != 0. Call isHappy(68). return false
- sum != 0. Call isHappy(100). return false
- sum == 0. return true: this doesn't mean your method will return true. The recursive call in the stack somewhere returned it, but it's not anticipated in your method. Instead, you just tell your method to
return false
after you make another call toisHappy()
.
To fix this, as tripathiakshit also mentioned, you need to return the value of your recursive call:
class Solution {
public boolean isHappy(int n) {
int sum =0;
int digit =0;
while (n>0) {
digit =n%10;
sum=sum+digit*digit;
n=n/10;
System.out.println(sum);
}
if(sum==1) {
return true;
}
else {
return isHappy(sum);
}
}
}
I would also suggest taking some safety measures to prevent a stack overflow. You should check if there will be a case where your method will never find a happy sum. You can simply create a base case to prevent this:
class Solution {
public boolean isHappy(int n, int callCount) {
if(callCount > 10) return false; // Stopping recursion if we exceed 10 calls
int sum = 0;
int digit = 0;
while (n > 0) {
digit = n % 10;
sum = sum + digit * digit;
n = n/10;
System.out.println(sum);
}
if(sum == 1) {
return true;
}
else {
callCount++;
return isHappy(sum, callCount);
}
}
}
And then you can call isHappy(19, 0);
with the comfort of being safe from exceptions and/or infinite loops.
Upvotes: 1
Reputation: 644
Not returning the return value of isHappy(sum)
might be the issue. See if this works as expected:
class Solution {
public boolean isHappy(int n) {
if (n == 0) return false;
int digit, sum = 0;
while (n > 0) {
digit = n % 10;
sum += digit * digit;
n /= 10;
System.out.println(sum);
}
if(sum == 1) return true;
else return isHappy(sum);
}
}
Upvotes: 2
Reputation: 154
return false will be executed in the final call stack, as the value returned from isHappy(sum) is ignored.
Upvotes: 0