Welcome to Dream.In.Code
Become a C# Expert!

Join 150,395 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,033 people online right now. Registration is fast and FREE... Join Now!




Problem with math & recursion..

 
Reply to this topicStart new topic

Problem with math & recursion.., Stack Overflow? Infinite recursion...

marcells23
9 Mar, 2008 - 03:27 PM
Post #1

D.I.C Head
Group Icon

Joined: 22 Aug, 2007
Posts: 139



Thanked: 3 times
Dream Kudos: 125
My Contributions
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;
            
                        

            
            
            
            


        }
    }

User is offlineProfile CardPM
+Quote Post

skaoth
RE: Problem With Math & Recursion..
9 Mar, 2008 - 04:46 PM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Nov, 2007
Posts: 357



Thanked: 12 times
Dream Kudos: 100
My Contributions
There is no gurantee that the recursion will ever terminate.

The reason 37 works is becaus
==============================
S(37) = 58
S(58) = 74
S(74) = 65
S(65) = 61
S(61) = 37


There is not guarantee that this is true

Let D1, D2 ... DN be the digits of X
X = S(X) = D1^2 + D2^2 + ... DN^2;

What happens when X == 1?
Lets assume we start with a value X = 10

S(10) = 1
S(1) = 1
S(1) = 1
.... Infinity

X will never have a value of 10. This
is most what is happening.

The other thing to consider is whether the
number will never reduce properly

X = 5.

S(5) = 25
S(25) = 29
S(29) = 85
S(85) = 89 <--- Problem starts here
S(89) = 145
S(145) = 42
S(42) = 20
S(20) = 4
S(4) = 16
S(16) = 37
S(37) = 58
S(58) = 89 <--- X will no longer reduce

good luck
User is offlineProfile CardPM
+Quote Post

papuccino1
RE: Problem With Math & Recursion..
9 Mar, 2008 - 04:48 PM
Post #3

D.I.C Head
**

Joined: 2 Mar, 2008
Posts: 91


My Contributions
I have one thing to add. Are you sure your "count++" is in the correct place? It's ouside the "if" and "for" functions.

Check that and see if it works.

I really don't understand the logic of your program, so this is all I can do. Maybe you could add some comments to what each part does.
User is offlineProfile CardPM
+Quote Post

orcasquall
RE: Problem With Math & Recursion..
12 Mar, 2008 - 06:25 AM
Post #4

D.I.C Head
Group Icon

Joined: 14 Sep, 2007
Posts: 158



Thanked: 3 times
Dream Kudos: 50
My Contributions
Use this instead
csharp

public static int smallestResult(int param0)
{
int count = 0;
int firstNum;
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());
}
return result;
}

You don't need to do recursion. The for loop is enough.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 06:16PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month