c++collatz

Why does this code not work with the integer 113383?


It's to find out how many operations are needed until the integer is calculated to 1 (Collatz problem).

It works with every integer except 113383. With 113383 the console just doesn't do anyhting, it doesn't print ops, it seems as the program is caught in a loop. Why?

I am using Visual Studio.

#include <iostream>

int main()
{
    int num = 113383;
    int ops = 0;
    while (num != 1) {
        if (num % 2 == 0)
        {
            num /= 2;
            ops++;
        }
        else if (num % 2 == 1)
        {
            num = num * 3 + 1;
            ops++;
        }
    }
    std::cout << ops << std::endl;
    return 0;
}

Solution

  • Because of integer overflow: I've shown intermediate values of num and ops and you see that the values become negative, which is typical for an overflow, as you can see:

    New version (with extra output):

    #include <iostream>
    
    int main()
    {
        int num = 113383;
        int ops = 0;
        while (num != 1) {
            if (num % 2 == 0)
            {
                num /= 2;
                ops++;
                std::cout << "num=" << num << std::endl;
                std::cout << "ops=" << ops << std::endl;
            }
            else if (num % 2 == 1)
            {
                num = num * 3 + 1;
                ops++;
                std::cout << "num=" << num << std::endl;
                std::cout << "ops=" << ops << std::endl;
            }
        }
        std::cout << ops << std::endl;
        return 0;
    }
    

    Result:

    num=340150
    ops=1
    num=170075
    ops=2
    num=510226
    ops=3
    num=255113
    ops=4
    num=765340
    ops=5
    num=382670
    ops=6
    num=191335
    ops=7
    num=574006
    ops=8
    num=287003
    ops=9
    num=861010
    ops=10
    num=430505
    ops=11
    num=1291516
    ops=12
    num=645758
    ops=13
    num=322879
    ops=14
    num=968638
    ops=15
    num=484319
    ops=16
    num=1452958
    ops=17
    num=726479
    ops=18
    num=2179438
    ops=19
    num=1089719
    ops=20
    num=3269158
    ops=21
    num=1634579
    ops=22
    num=4903738
    ops=23
    num=2451869
    ops=24
    num=7355608
    ops=25
    num=3677804
    ops=26
    num=1838902
    ops=27
    num=919451
    ops=28
    num=2758354
    ops=29
    num=1379177
    ops=30
    num=4137532
    ops=31
    num=2068766
    ops=32
    num=1034383
    ops=33
    num=3103150
    ops=34
    num=1551575
    ops=35
    num=4654726
    ops=36
    num=2327363
    ops=37
    num=6982090
    ops=38
    num=3491045
    ops=39
    num=10473136
    ops=40
    num=5236568
    ops=41
    num=2618284
    ops=42
    num=1309142
    ops=43
    num=654571
    ops=44
    num=1963714
    ops=45
    num=981857
    ops=46
    num=2945572
    ops=47
    num=1472786
    ops=48
    num=736393
    ops=49
    num=2209180
    ops=50
    num=1104590
    ops=51
    num=552295
    ops=52
    num=1656886
    ops=53
    num=828443
    ops=54
    num=2485330
    ops=55
    num=1242665
    ops=56
    num=3727996
    ops=57
    num=1863998
    ops=58
    num=931999
    ops=59
    num=2795998
    ops=60
    num=1397999
    ops=61
    num=4193998
    ops=62
    num=2096999
    ops=63
    num=6290998
    ops=64
    num=3145499
    ops=65
    num=9436498
    ops=66
    num=4718249
    ops=67
    num=14154748
    ops=68
    num=7077374
    ops=69
    num=3538687
    ops=70
    num=10616062
    ops=71
    num=5308031
    ops=72
    num=15924094
    ops=73
    num=7962047
    ops=74
    num=23886142
    ops=75
    num=11943071
    ops=76
    num=35829214
    ops=77
    num=17914607
    ops=78
    num=53743822
    ops=79
    num=26871911
    ops=80
    num=80615734
    ops=81
    num=40307867
    ops=82
    num=120923602
    ops=83
    num=60461801
    ops=84
    num=181385404
    ops=85
    num=90692702
    ops=86
    num=45346351
    ops=87
    num=136039054
    ops=88
    num=68019527
    ops=89
    num=204058582
    ops=90
    num=102029291
    ops=91
    num=306087874
    ops=92
    num=153043937
    ops=93
    num=459131812
    ops=94
    num=229565906
    ops=95
    num=114782953
    ops=96
    num=344348860
    ops=97
    num=172174430
    ops=98
    num=86087215
    ops=99
    num=258261646
    ops=100
    num=129130823
    ops=101
    num=387392470
    ops=102
    num=193696235
    ops=103
    num=581088706
    ops=104
    num=290544353
    ops=105
    num=871633060
    ops=106
    num=435816530
    ops=107
    num=217908265
    ops=108
    num=653724796
    ops=109
    num=326862398
    ops=110
    num=163431199
    ops=111
    num=490293598
    ops=112
    num=245146799
    ops=113
    num=735440398
    ops=114
    num=367720199
    ops=115
    num=1103160598
    ops=116
    num=551580299
    ops=117
    num=1654740898
    ops=118
    num=827370449
    ops=119
    num=-1812855948
    ops=120
    num=-906427974
    ops=121
    num=-453213987
    ops=122
    

    I would advise you to go for larger integer types, like long, long long, or even larger.