I am doing projecteuler problem 14 and i thought i had it down but for some reason i cant get my while loop to stop running when i = 1. i have tried using a temp variable to make sure im not changing i = 1 and tried using both
while (i!=1)
and also
while (i>1)
when i run the program it get the first value for i down to 1 then it starts returning 1,4,2,1,4,2,1,4,2 over and over. nothing is working. any help would be appreciated.
#include <iostream>
usingnamespace std;
long i;
int mosttimes = 0;
int count = 0;
long chainnumber = 0;
long temp;
int main()
{
for (i=13;i<1000000;i++)
{
count = 0;
temp = i;
cout << i << endl;
while (temp>1) // while i is greater than 1
{
if (temp%2==0) //if i is even
{
temp = temp/2;
cout << "i now equals " << temp << endl;
system ("pause");
}
if (temp%2==1) //if i is odd
{
temp = 3*temp+1;
cout << "i now equals " << temp << endl;
system ("pause");
}
count = count++; //counts the number of
}
if (count > mosttimes)
{
mosttimes = count;
chainnumber = i;
cout << "the number" << chainnumber << "had a chain length of " << mosttimes << endl;
}
}
}
#include <iostream>
usingnamespace std;
int i;
int mosttimes = 0;
int count = 0;
int chainnumber = 0;
int temp;
int main()
{
for (i=13;i<1000000;i++)
{
count = 0;
temp = i;
cout << i << endl;
while (temp>1) // while i is greater than 1
{
if (temp%2==0) //if i is even
{
temp/=2;
cout << "i now equals " << temp << endl;
}
else //if i is odd
{
temp = 3*temp+1;
cout << "i now equals " << temp << endl;
}
count++;
system("pause"); //counts the number of
}
if (count > mosttimes)
{
mosttimes = count;
chainnumber = i;
cout << "the number" << chainnumber << "had a chain length of " << mosttimes << endl;
}
}
}
edit : btw long temp or int temp basically is long integer i think, cmiiw
and you can use