Problem with my code

I've been digging at this for a while, and I just can't quite figure out what I'm doing wrong. After staring at my code for about an hour, I decided to just see if anyone else could spot the obvious problem.

I'm attempting to complete Project Euler Problem #14, and it's a simple enough concept just very error-prone for me I suppose.

The problem is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
The following iterative sequence is defined for the set of positive integers:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

Using the rule above and starting with 13, we generate the following sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1

It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. 
Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

Which starting number, under one million, produces the longest chain?

NOTE: Once the chain starts the terms are allowed to go above one million.


My code is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>

using namespace std;

int main()
{
    //define the variables
    long long startVal;
    int counter;
    int counterhigh = 0;
    int countmax = 0;

    //loop runs so long as the starting number isn't greater than 999,999
    for(startVal = 10; startVal < 1000000; startVal++)
    {
        //run loop as long as startVal is greater than 1, because if it gets down to 1 it will run infinitely
        do
        {
            //if the number is even, divide it by two, count every action done to measure the chain
            if(startVal%2 == 0)
            {
                startVal = startVal / 2;
                counter++;
                if(counter > counterhigh)
                {
                    counterhigh = counter;
                    countmax = startVal;
                }
            }

            //if the number is odd multiply by 3 and add 1, count every action done to measure the chain
            else if(startVal%2 != 0)
            {
                startVal = (startVal*3)+1;
                counter++;
                if(counter > counterhigh)
                {
                    counterhigh = counter;
                    countmax = startVal;
                }
            }
        }
        while (startVal > 1);

        //you can use this to see what startVal is outputting, for some odd reason it always seems to be "1", and startVal 
        //is at no point in the program being assigned the value of 1.
        //cout << startVal << endl; 

        //reset the counter for each chain
        counter = 0;
    }
    
    //output the greatest chain length
    cout << "The greatest is: " << countmax << endl;
    cin.get();
    cin.get();
    return 0;
}
There are a few problems here. First, counter is used before it is initialized. Second, you are changing the value of startVal in the while loop, which breaks the outer for loop. Try using a separate variable for each loop.
Did that with similar results. New code is as follows, but same output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>

using namespace std;

int main()
{
    //define the variables
    long startVal = 0;
    long _val = 0;
    int counter = 0;
    int counterhigh = 0;
    int countmax = 0;

    //loop runs so long as the starting number isn't greater than 999,999
    for(startVal = 10; startVal < 1000000; startVal++)
    {
        _val = startVal;
        //run loop as long as startVal is greater than 1, because if it gets down to 1 it will run infinitely
        do
        {
            //if the number is even, divide it by two, count every action done to measure the chain
            if(_val%2 == 0)
            {
                _val = _val / 2;
                counter++;
                if(counter > counterhigh)
                {
                    counterhigh = counter;
                    countmax = _val;
                }
            }

            //if the number is odd multiply by 3 and add 1, count every action done to measure the chain
            else if(_val%2 != 0)
            {
                _val = (_val*3)+1;
                counter++;
                if(counter > counterhigh)
                {
                    counterhigh = counter;
                    countmax = _val;
                }
            }
        }
        while (_val > 1);

        //reset the counter for each chain
        counter = 0;
    }

    //output the greatest chain length
    cout << "The greatest is: " << countmax << endl;
    cin.get();
    cin.get();
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <cstdio>
using namespace std;

// n → n/2 (n is even)
// n → 3n + 1 (n is odd)

void main()
{
	__int64 Biggest = 0, Bigcount = 0;
	for(__int64 x=2;x<1000000;x++)
	{
		__int64 Count = 0, number = x;
		do
		{
			if((number%2) == 0)
				number/=2;
			else
				number = number * 3 + 1;
			Count++;
		}while(number != 1);
		if(Count > Bigcount)
		{
			Bigcount = Count;
			Biggest = x;
		}
	}

	cout << "The highest number of possible changes was " << Biggest << " with " << Bigcount << " changes!" << endl;
	getchar();
}


And your answer is 837799 with a total of 524 transactions. I would add error correcting of course!
Last edited on
Topic archived. No new replies allowed.