- #1
chaoseverlasting
- 1,050
- 3
Im trying to find the maximum cycle length, but the program hangs when i>100000 or so. Here's the code:
#include<iostream>
using namespace std;
int main()
{
unsigned int i, j;
int count, cmax;
int process(int x);
cout<<"Enter range:\nFrom: ";
cin>>i;
cout<<"\nTo: ";
cin>>j;
cout<<i<<" "<<j;
for(;i<=j;i++)
{
count=process(i);
if(count>cmax)
cmax=count;
}
cout<<" "<<cmax;
return 0;
}
int process(int x)
{
int count;
for(count=1;x!=1;++count)
{
if(x%2==0)
x/=2;
else
x=3*x+1;
}
return count;
}
I think the values become too large for integers to handle. I was told to assume that the values would not exceed 32 bits, aren't integers 2 bits though?
#include<iostream>
using namespace std;
int main()
{
unsigned int i, j;
int count, cmax;
int process(int x);
cout<<"Enter range:\nFrom: ";
cin>>i;
cout<<"\nTo: ";
cin>>j;
cout<<i<<" "<<j;
for(;i<=j;i++)
{
count=process(i);
if(count>cmax)
cmax=count;
}
cout<<" "<<cmax;
return 0;
}
int process(int x)
{
int count;
for(count=1;x!=1;++count)
{
if(x%2==0)
x/=2;
else
x=3*x+1;
}
return count;
}
I think the values become too large for integers to handle. I was told to assume that the values would not exceed 32 bits, aren't integers 2 bits though?