0

Im trying to solve the Collatz problem. It's all working except of one my int highest which should compare whether counter of one number is greater than counter of next number seems not to be working. I also tried my highest variable as array but still getting no result. Thanks for your any advice.

#include <vector>
#include <iostream>

using namespace std;

int main()
{

    __int64 num;
    int i;
    int counter=0;
    //vector<int> a(1000000);
    //vector<int> b(1000000);

    __int64 highest=0;
    int j=0;

    for(j=2;j<=1000000;j++)
    {
        num=j;
        counter=0;

        for(i=0;i<1000000;i++)
        {
            cout<<"num is: "<<j<<endl;

            if(num==1)
            {
                break;
            }

            if(num%2==0)
            {
                num = num/2;
                counter++;
                cout<<num<<endl;
            }
            else if(num%2!=0)
            {  
                counter++;
                num= (num*3)+1;
                cout<<num<<endl;
            }
        }

        cout<<"counter: "<<counter<<endl;

        //this part is not working 
        if(highest<=counter)
        {
            highest=counter;
            cout<<"highest is: "<<highest<<endl;
        }

    }

    return 0;
}
Bridge
  • 29,818
  • 9
  • 60
  • 82
Peter F
  • 83
  • 2
  • 3
  • 12
  • I've tried to fix your code formatting - if you want people to help, please bother to do this yourself in future. One more word of advice - people may not be familiar with what the `Collatz sequence` is - so it might be a good idea to include an explanation (or a link to one). – Bridge Mar 15 '13 at 09:21
  • See http://stackoverflow.com/questions/2643260/project-euler-question-14-collatz-problem. Is for Euler project? – Mihai8 Mar 15 '13 at 09:21

0 Answers0