### program stands still SO CONFUSED

Hey guys this is really messing with my head. Thanks in advance for any help yall can give me.

I am trying to write a simple number sequence type program for someone.. this code compiles and runs... but for some reason, I have no idea why, the program just seems to get stuck between two lines of code. I used stuff like:

`if (x==113383){cout << "Line 00";}`
to find out where it was going wrong, and I've inserted a comment into the code below. The line which preceeds the comment runs just fine, but the line after the comment apparently never happens when (x == 113383)!

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#include using namespace std; int run_sequence(int x); int main() { int largest, terms_ret, terms_max; terms_max = 0; largest = 0; for (int quantum=1; quantum < 1000000; quantum++) { terms_ret = run_sequence(quantum); if (terms_ret > terms_max) { terms_max = terms_ret; largest = quantum; cout << "New longest sequence: " << largest << '\n'; cout << "Terms: " << terms_max << "\n"; } } cout << "The number between 1 and 1000000 which starts the longest sequence is: " << largest; return 0; } int run_sequence(int x) { int cycle, w; cycle = 0; while (x != 1) { w = (x % 2); // here's where I lose it when x == 113383 (wtf?) if (w == 0) { x = (x / 2); } else { x = ((x * 3) + 1); } cycle++; } return cycle; }``````

What goes on here? I am so lost.. is this an unusual problem?

Thanks for any suggestions.
- cPlusN()()b

p.s. I have used long int too, makes no difference, tho I didn't expect it to..
the statement: `w = (x % 2);`
runs, then, nothing. :/
Last edited on
UPDATE ok so
I used unsigned int for quantum and x and now it works perfectly. I think.

Can anyone please explain whats going on here? after all 1000000 is much more than 2 * 113383, and the value of x should never be negative, so why is the signed/unsigned causing trouble?
I'm scared.
Last edited on
 I am trying to write a simple number sequence type program for someone..

This is one of the Euler problems. Have someone write his own code.

 What goes on here? I am so lost.. is this an unusual problem?

Unfortunately, no. Not that unusual.

Don't use signed values where unsigned values are called for. The condition you should've been looking for on line 32 is `x < 0`.

 I have used long int

long int and int hold the same range of values on your system. long long would've worked. Or.. just an unsigned int.

Btw, your count will be off by one.
` for (int quantum=1; quantum < 1000001; quantum++) `

that better? lol

btw why should I check for a negative value of x?
nevermind, I think I see what ya meant

thanks for helping me with this, cire
Last edited on
Because when you add too much to a variable, this variable will overflow resulting in negative values.

Try a test like:

 ``123456`` ``````int Test = 0; while(Test > 0) { Test++; } cout << "Hey, Test overflowed to value " << Test << std::endl;``````
I just ran some debugging, and i'm so close to fixing it. When it reaches 113383 or near it, w starts to go negative......
I think, 113383 is a special number, and it runs your program forever and then your program crashes and freezes...
already fixed the problem green ^

I made "quantum" and "x" unsigned int
thanks for all the help peoples
Last edited on
Wait, how did that help? Sorry, I don't use those terms much.
Topic archived. No new replies allowed.