while loop not working

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.

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
#include <iostream>

using namespace 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;
		}

	}
}
count = count ++; isn't necessary,
just count++;

you can try this code while searching for another solution, good luck.

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
#include <iostream>

using namespace 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

temp/=2;

rather than

temp = temp / 2;
Last edited on
thanks for the tips! =)
Topic archived. No new replies allowed.