# Thread: infinate loop that apparently shouldnt happen

1. ## 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. 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. 3. ODD is 2n+1, not 3n+1. 4. euler said 3n.... n → 3n + 1 (n is odd) < 5. maybe its a typo ill try 2n 6. nope thats worse if never calls the even function and maxes out after 20 or so loops 7. Are you certain that it is "x/2" instead of "x/3"?

Tim S. 8. 9. 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. 10. 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. 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 chain, int, long, return, unsigned 