i've been asked this question somewhere .. you are given 2 integers l and r ..your task is to determine sum of all beautiful numbers in the range land r.
a number is a beautiful number if it satisfies the following conditions
if a number becomes one at some point by replacing it repeatedly with the sum of squares of its digits. NOTE : if the number never becomes one then the provided number is not a beautiful number
example : For example, 32 is a happy number as the process yields 1 as follows
3^2 + 2^2 = 13
1^2 + 3^2 = 10
1^2 + 0^2 = 1
for range (31,32)...the answer is 31+32 = 63 ..as both are beautiful numbers.
i tried to do a recursive apporch like this :
recursivefunction(int num){
if(num == 1) return true;
//Calculates the sum of squares of digits
while(num > 0){
rem = num%10;
sum = sum + (rem*rem);
num = num/10;
}
recursivefunction(sum);
}
calling this function recursively for a range and stored the value in a sum if its 1 ..then add it into a sum variable.
and in question there was no breaking case , like what i have to do when i don't fount 1 ...so i put a counter like if it goes into recursion 10 times and still not 1 then return false;
but the thing is this function is giving time out in some if the test cases .