c++ - Reverse Collatz sequence code not computing -


using c++, writing program take first n numbers starting @ 1 , output respective "path count", call number of iterations of collatz sequence takes number 0 (see wikipedia article on collatz conjecture). example, path number of 8 3, because takes 3 steps 1 (8 : 2 = 4; 4 : 2 = 2; 2 : 2 = 1).

thus have simple function:

int pathnumber(int n){     int pathcount = 0;     while(n > 1){         if(n % 2 == 0){             n /= 2;         }else{             n *= 3;             n ++;         }         pathcount ++;     }     return pathcount; } 

i decided write inverse function of (called firstinstanceof(n)), compute first instance of given pathnumber in ascending order 1. example, firstinstanceof(3) 8, because 8 first number in ascending order 1 pathcount 3. function wrote follows:

int firstinstanceof(int s){     int = 0;     while(pathnumber(i) != s){         ++;     }     return i; } 

where s input value , variable increments 1 each time. simple seems, code compiles without warnings not compute. have tried reformatting latter function loop, infinity loop, etc. still program persists in either buffering indefinitely or outputting bogus values. feel missing obvious? appreciated.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -