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.
#include <iostream>
usingnamespace std;
int main()
{
//define the variables
longlong 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
elseif(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.
#include <iostream>
usingnamespace 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
elseif(_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;
}