problem on submitting 3n+1 problem

i am a beginner . i am trying to submit a solution of 3n+1 problem.it works properly but when i submit this , wrong answer is replied.please help me to solve this .

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main(){

long int start,end,temp=0;

long int value[1000000];

string startvalue,endvalue;

while(cin>>start>>end){
//cin>>start>>end;
//getline(cin,startvalue,endvalue);
//stringstream(startvalue) >> start;
//getline(cin,endvalue);
//stringstream(endvalue) >> end;
//cout<<"\t";

for(int i=start;i<=end;i++){

long int j=i;
value[i]=1;

if(j==1){
temp=value[i];
continue;
}


else{

while(j>1){

if ((j%2)!=0){
j=3*j+1;

//end of if ((i%2)!=0)
}

else
{j=j/2;}

value[i]++;
}
}

if(i>start&&value[i]>temp){temp=value[i];}
if(i==start){temp=value[i];}

}

cout<<start<<" "<<end<<" "<<temp<<"\n";

}
return 0;
//end of main loop




}
a) Please use code tags. It is "<>" button
b) What 3n+1 problem is?

Probably you have problems with output format. Remember: any missing or excessive whitespace or newline can lead to rejection.
long int value[1000000]; is too big, it may cause a stack overflow.
Resize it to a decent size, like 1000, 2000 or so.
while(cin>>start>>end)
debería ser
1
2
3
cout <<"enter star and end";
cin>>start;
cin>>end;

what is in parenthesis is a expression
while (start <end)
start is less than end
while (start > end)
start is greater than end
http://www.cplusplus.com/doc/tutorial/control/

for(int i=start;i<=end;i++)
int Integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295

int i is not equal to start.
stack overflow
http://www.cplusplus.com/doc/tutorial/variables/
Last edited on
@flony1
This is a correct and safe way to read values until the end of stream.
He did everything right here.
i don't know if you're about going through the CSUS, but in all cases execution time is really important in such problem, i happened to have a look at the problem -really challenging it is-, your algorithm to statically allocate 1million variables in try to store all the sequence is really capable of frying lots of computers.
i suggest looking for another algorithm ASAP.
> What 3n+1 problem is?
Details.

long int is may be too short
Last edited on
Topic archived. No new replies allowed.