Define the function S(x) as the sum of the squares of the digits of x.
For example: S(3)=3*3=9 and S(230)=2*2+3*3+0*0=13.
basically my code takes in an integer and does the above recursively until it reaches the first number we started with. for some reason it keeps giving me a stack overflow at the
Int32.Parse. It also works if I input the number 37..maybe its something simple im not seeing. any clues?
CODE
public class SquareDigits
{
static int count = 0;
static int firstNum;
public static int smallestResult(int param0)
{
if (count == 0)
{
firstNum = param0;
}
int result = 0;
char[] digits = param0.ToString().ToCharArray();
count++;
for (int i = 0; i < digits.Length; ++i)
{
result += Int32.Parse(digits[i].ToString()) * Int32.Parse(digits[i].ToString());
}
if (result == firstNum)
{
Console.WriteLine(count);
return 0;
}
else
{
return smallestResult(result);
}
return 0;
}
}