Strange Problem

I've never run into this problem before. The routine stops in the middle of execution @ the number 113383. If you inspect the code you'll see that the stopping condition in the for loop has clearly not been satified. To be honest, I'm baffled. I even ran the code on my other comp. Same results. My request is: someone paste the code into your IDE (or wherever) and see if the same thing happens.
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
//#include <ttmath/ttmath.h>
#include <iostream>
#include <conio.h>

#define MAX 1000000
//n = n/2 ( when n is even)
//n = 3n + 1 ( when n is odd)
using namespace std;

int main ()
{
   // Collatz Problem
   // HOTPO = Half or Triple Plus One
   // count = how many HOTPO calculations until HOTPO reaches one
   long HOTPO = 0, count = 0;

   for (int i = 100; i < MAX; i++){
       HOTPO = i;
       count++;
       cout << HOTPO << " --> ";
       do{
           if ( HOTPO % 2 != 0 ){
             HOTPO = (3*HOTPO) + 1;
             count++;
           }
           else{
             HOTPO /= 2;
             count++;
           }
       }while (HOTPO != 1);
       cout << count << endl;
       count = 0;
   }
   return 0;
}
Last edited on
It doesn't stop, it gets into an infinte loop, since long overflows and value of HOTPO gets negative for 113383.
Changing long to unsigned long works for 113383. I would suggest to use some bignum library if you want to continue (ttmath is a good start :) ).
*bows* I used unsigned int long etc... thousands of times. Why I didnt use it here is beyond me. Thx ROmai.

PS ttmath is awesome. )
Last edited on
Umm, your algorithm says 4 for 1. Isn't that, like, wrong?
The routine works perfectly after I changed long HOTPO to unsigned long.
Not sure what you mean vilml, by '4 for 1'. Explain.
Topic archived. No new replies allowed.