// Iterates the function 'sumsquaredigits' until it reaches state N=1 #include using namespace std; // This function assumes positive n int sumsquaredigits (int n) { int answer = 0 , rem ; while ( n > 0 ) { rem = n%10 ; // the remainder is the next digit (Reading from the right) answer += rem * rem ; n = n/10 ; } return answer ; } int threemap (int n) { if (n%2) // n is odd return (3*n+1); else // n is even return (n/2) ; } void iterate( int (*function_to_iterate)(int) , int N , int I , int match1, int match2 ) { cout << N << endl ; // starting value for ( int i = 1 ; i <= I ; i ++ ) { N = function_to_iterate( N ) ; cout << N << " " ; if ( N == match1 || N == match2 ) { cout << "\n(" << i << " iterations)\n" ; return ; } if ( !(i%10) ) cout << endl ; } } int main(int argc, char* argv[]) { int N=12; // starting number int I=1000; // maximum number of iterations if(argc>1) { sscanf( argv[1], "%d", &N ) ; // put the first command-line argument in N } cout << "Running threemap from "<< N <