Created
February 9, 2014 09:00
-
-
Save yfe404/8896400 to your computer and use it in GitHub Desktop.
Collatz conjecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
unsigned int syracuse(unsigned int n) | |
{ | |
if(n%2 == 0) | |
return n/2; | |
else | |
return 3*n + 1; | |
} | |
int main(int argc, char* argv[]) | |
{ | |
unsigned int number = 1; | |
if(argc != 2){ | |
fputs("Usage : ./syracuse number\n", stderr); | |
return EXIT_FAILURE; | |
} | |
sscanf(argv[1], "%d$", &number); | |
while(number > 1){ | |
printf("%d->", number); | |
number = syracuse(number); | |
} | |
printf("%d\n", number); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment