Skip to content

Instantly share code, notes, and snippets.

@rustyconover
Created May 5, 2015 21:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rustyconover/0319ee8d6841b8d5ef39 to your computer and use it in GitHub Desktop.
Save rustyconover/0319ee8d6841b8d5ef39 to your computer and use it in GitHub Desktop.
Collatz sequence in C
#include <stdio.h>
int collatz_count_until_1(unsigned int n) {
int count = 0;
while(n != 1) {
if(n % 2 == 0) {
n /= 2;
} else {
n = (3 * n) + 1;
}
count++;
}
return count;
}
int main(void) {
int length = 0;
int number = 0;
for(int i = 1; i <= 300000; i++) {
int l = collatz_count_until_1(i);
if(length < l) {
length = l;
number = i;
}
}
printf("Maximum stopping distance %d, starting number %d\n", length, number);
}
@CORDEIRO1286
Copy link

Thanks,your code helped me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment