39 lines
611 B
C
39 lines
611 B
C
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <signal.h>
|
|
#include <unistd.h>
|
|
|
|
int counter=1;
|
|
|
|
void handler(int sig){
|
|
if(sig == SIGINT){
|
|
printf("Early ending. the current ammount of application is : %d\n", counter);
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
int collatz(int n){
|
|
sleep(1);
|
|
if (n % 2 == 0)
|
|
return n / 2;
|
|
return 3 * n + 1;
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
signal(SIGINT, handler);
|
|
if (argc != 2)
|
|
exit(1);
|
|
int test = atoi(argv[1]);
|
|
printf("%d\n", test);
|
|
while(test != 1){
|
|
counter++;
|
|
test = collatz(test);
|
|
printf("%d\n", test);
|
|
}
|
|
printf("---\n%d\n", counter);
|
|
return 0;
|
|
}
|
|
|