Skip to content

Instantly share code, notes, and snippets.

@wizo06
Last active June 22, 2018 00:02
Show Gist options
  • Save wizo06/bd2de282df41a4fd545aa7c3a5a9a59d to your computer and use it in GitHub Desktop.
Save wizo06/bd2de282df41a4fd545aa7c3a5a9a59d to your computer and use it in GitHub Desktop.
To run: Compile and "./collatz <integer>". Collatz Conjecture in C++. Left column is the number of steps to get to 1.
// Warning: maximum value for a variable of type int: 2,147,483,647
#include <stdio.h>
int main(int argc, char *argv[]){
if(argc == 2){
int x = atoi(argv[1]); int n = 0; printf("%i %i\n",n,x);
if(x < 1){
exit(1);
}
else{
while(x != 1){
if(x%2 == 0){
x = x/2; n++; printf("%i %i\n",n,x);
}
else{
x = (3*x)+1; n++; printf("%i %i\n",n,x);
}
}
}
}
else{
exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment