Created
May 5, 2015 21:33
-
-
Save rustyconover/0319ee8d6841b8d5ef39 to your computer and use it in GitHub Desktop.
Collatz sequence in C
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> | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks,your code helped me