Thanks Epy. These are really good suggestions.
I updated code according to these.
Code:
#include <stdio.h>
int f(int i);
int main()
{
unsigned long long terms[1000001];
unsigned long long i;
unsigned long long max = 0;
terms[0] = terms[1] = 0;
unsigned long long step;
unsigned long long temp;
for(i = 2; i < 1000000; i++)
{
step = 0;
temp = i;
//printf("%llu",i);
do
{
temp = f(temp);
step++;
//printf("->%llu",temp);
}
while(temp > i - 1);
terms[i] = step + terms[temp];
max = terms[i]>max?terms[i]:max;
printf("\n%llu,%llu,%llu\n",i,terms[i],max);
}
printf("%llu\n",max);
return 0;
}
int f(int n)
{
return (n%2 == 0) ? n/2 : 3*n+1;
}
Now the code is running and it seems it's running forever when reaching the same value.
Last few lines of output:
Code:
113380,66,353
113381,66,353
113382,66,353