Files
2024-10-31 14:30:55 -04:00

37 lines
1.0 KiB
C

# include <stdio.h>
# include <stdint.h> // for uint32_t
# include <inttypes.h> // for PRIu32 in printf
// " Half or triple - plus - one " sequence , iterative .
uint32_t hotpo_i ( uint32_t n ) {
printf ("%" PRIu32 "\n" , n ) ;
while ( n > 1) {
if ( n % 2 == 0) n /= 2;
else n = 3 * n + 1;
printf ("%" PRIu32 "\n" , n ) ;
}
return n ;
}
// " Half or triple - plus - one " sequence , recursive .
uint32_t hotpo_r ( uint32_t n ) {
static uint32_t recur_depth = 1; // recursion depth
uint32_t next_n ;
printf ("%" PRIu32 "\t %" PRIu32 "\n ",n,recur_depth) ;
if ( n == 1) return 1;
recur_depth++;
if ( n % 2 == 0) next_n = hotpo_r ( n /2) ;
else next_n = hotpo_r (3* n + 1) ;
recur_depth--;
return next_n ;
}
// Read an unsigned integer from stdin and invoke both versions of
// hotpo on it .
int main () {
uint32_t init_n;
scanf ("%"PRIu32, &init_n) ;
hotpo_i ( init_n ) ;
putchar ('\n') ;
hotpo_r ( init_n ) ;
return 0;
}