My initial question was a bit vague, so here's the edited question.
The 3n+1 uVa problem is based on the Collatz Conjecture. Consider any positive integer 'n'. If it is even, divide it by 2. If it is odd, multiply it by 3 and add 1. After 'x' such repeated operations, you will get 1. This has been proven for very large numbers by supercomputers.
UVa Online Judge is an online automated judge for programming problems hosted by University of Valladolid.
Here's a link to the problem statement: uVa 3n+1
Please read the problem statement before looking at my code! The uVa online judge runs your code against certain test-cases and tells you if your solution was right or wrong. It does not tell you why it failed (if your solution was wrong).
Here is the link again if you missed the first one: The link to the problem description
I don't understand what I am missing out or skipping (since I am getting Wrong Answer) in my code logic. I have tried lots of test cases, and they seem to be working fine. I know the logic can be compressed to a large extent, but I just need to figure out where the flaw is for now.
Here is my code. I know bits/stdc++ shouldn't be used, but currently, it's not affecting my code. Please review it if you can and help me out.
#include<iostream>
#include<cstdlib>
#include<bits/stdc++.h>
using namespace std;
int main()
{
unsigned long int num;
int i,j,tempi, tempj,temp;
//Scanning until inputs don't stop
while(scanf("%d %d",&i,&j)!=EOF)
{
//Boolean variable to set if i is greater
bool iwasmore=false;
//Boolean variable for equal numbers
bool equalnums=false;
int cycles=1, maxcycles=0;
if(i>j)
{
//swapping for the for loop to follow
temp=i; i=j; j=temp;
iwasmore=true;
}
if(i==j)
{
equalnums=true;
}
tempi=i; tempj=j;
//Taking each number in the given range and running it in the algorithm.
//The maxcycles variable will have the value of the maximum number of cycles
//that one of the numbers in the range took (at the end of the for loop).
for(num=i;num<=j;num=(++tempi))
{
if(cycles>maxcycles)
{
maxcycles=cycles;
}
cycles=1;
//The actual algorithm
while(num!=1)
{
if(num%2==1)
{
num = (3*num)+1;
cycles++;
}
else
{
num=(num/2);
cycles++;
}
}
if(equalnums==true)
{
maxcycles=cycles;
equalnums=false;
}
//Resetting num
num=0;
}
if(!iwasmore)
cout<<i<<" "<<j<<" "<<maxcycles<<endl;
else
cout<<j<<" "<<i<<" "<<maxcycles<<endl;
}
return 0;
}
Here is the output I am getting for the following input:
Input:
1 1
10 1
210 201
113383 113383
999999 1
Output:
1 1 1
10 1 20
210 201 89
113383 113383 248
999999 1 525
Another testcase:
Input:
1 5
10 8
210 202
113383 113383
999999 999989
Output:
1 5 8
10 8 20
210 202 89
113383 113383 248
999999 999989 259