Im currently making a program that will return the values of 3x+1 given starting value x.
Here is the trimmed code.
//3x+1
//starting at any positive integer (x)
//multiply by 3 then add one
//(n%2==0?/2:3n+1)
std::vector<int> Holder;
int usrNum;
std::cin >> usrNum;
Holder.push_back(usrNum);
Holder.push_back(3*usrNum+1);
for(int i = 1; i < 200; i++){
(Holder[i]%2==0?Holder.push_back(Holder[i]/2):Holder.push_back(3*Holder[i]+1));
}
for (int i = 0; i < Holder.size(); i++) {
std::cout << Holder[i] << " ";
}
currently im using 200 as a hard coded value because if i use i < Holder.size() it causes an infinite loop and i never catches up.
anyone have any ideas as to how i can make it loop until it reaches the value 1?
>Solution :
Use a while loop, with the termination condition being when the last element is 1. Have a look at this reference for std::vector for help.