3n+1 problem solving program in CPP
#include <iostream>
#include<conio.h>
using namespace std;
int cycle(long int n)//LOW
{
long int i=1;
while(n!=1)
{
if(n%2 == 0)
{
n=n/2;
}
else
{
n=(3*n)+1;
}
i++;
}
return i;
}
int main()
{
int a,b,low,high,count;
std::cout << "Enter the series of numbes:" << std::endl;
while(std::cin >> a>>b)
{
if(a<b)
{
low=a;
high=b;
}
else
{
low=b;
high=a;
}
int max = cycle(low);
for(int i=low+1;i<=high;i++)
{
int l=cycle(i);
if(l>max)
{
max=l;
}
}
cout<<a<<" \t "<<b<<" \t "<<max<<"\n";
}
}
output:
No comments:
Post a Comment