Thread: infinate loop that apparently shouldnt happen

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    662

    infinate loop that apparently shouldnt happen

    i have this code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define LIMIT 1000000
    
    int even_num(long long unsigned int x);
    int odd_num(long long unsigned int x);
    
    int main()
    {
        int i, tmp_chain, chain, result;
        long long unsigned int x;
    
        for (i = 1; i < LIMIT; i++)
        {
            tmp_chain = 0;
            x = i;
            while (x > 1)
            {
                if (x % 2 == 0) // i is even
                {
                    x = even_num(x);
                }
                else
                {
                    x = odd_num(x);
                }
                tmp_chain++;
            }
            if (tmp_chain > chain)
            {
                result = i;
                chain = tmp_chain;
            }
        }
    
        printf("result is %d and chain is %d\n", result, chain);
        return 0;
    }
    
    int even_num(long long unsigned int x)
    {
        return x/2;
    }
    
    int odd_num(long long unsigned int x)
    {
        x *= 3;
        x += 1;
        return x;
    }
    i worked this out and then checked the answer i was wrong and found a couple of mistakes but now when i run it its an infinite loop. (i put the answer in and got up to a chain of over 3000) but cant for the life of me see what the issue is i think some of it is that x gets way too big before it starts getting to a sensible number
    coop

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    Start with the number 4 and it goes to 2 and then to 1 and then back to 4. I just did that in my head so it might be wrong. Looks like I was wrong at a second look.
    Last edited by stahta01; 06-06-2019 at 05:38 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    533
    ODD is 2n+1, not 3n+1.

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    euler said 3n.... n → 3n + 1 (n is odd) <


  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    maybe its a typo ill try 2n

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    nope thats worse if never calls the even function and maxes out after 20 or so loops

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    Are you certain that it is "x/2" instead of "x/3"?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    3,675
    Function signature is questionable

    Code:
    int even_num(long long unsigned int x)
    I think the type being returned should match the type of x.
    Do you have a reason to differ with this idea?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    no it should be the same i changed x from int to what it is now as i was getting negative numbers and forgot the return types

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    662
    thanks that was the issue i forgot to change the return types

    is there any reason that project euler uses such big numbers the program would be exactly the same if the numbers or limits were smaller.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Infinate loop that shouldn't be.
    By Blizard6 in forum C++ Programming
    Replies: 7
    Last Post: 06-15-2010, 02:35 PM
  2. shouldnt it ...
    By BlaX in forum C Programming
    Replies: 7
    Last Post: 02-22-2008, 01:23 PM
  3. Infinate Loop
    By sreetvert83 in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2005, 08:17 PM
  4. Infinate Loop, not for all
    By Erf in forum C++ Programming
    Replies: 1
    Last Post: 07-22-2004, 02:26 AM
  5. Infinate loop!!!
    By Munkey01 in forum C++ Programming
    Replies: 8
    Last Post: 01-07-2003, 07:31 PM

Tags for this Thread