forked from ravomavain/uselessness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collatz.c
45 lines (39 loc) · 813 Bytes
/
collatz.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#define START 1
#define END 1000000
#define REPORT 100000
typedef unsigned long int NUM;
NUM collatz_length(NUM number)
{
int result = 1;
while(number != 1)
{
if(number % 2 == 0)
number /= 2;
else
number = number * 3 + 1;
result++;
}
return result;
}
int main(int argc, char **argv)
{
int max_length_index = START;
int max_length = collatz_length(max_length_index);
int i;
for(i = START + 1; i < END; i++)
{
int length = collatz_length(i);
if(length > max_length)
{
max_length = length;
max_length_index = i;
printf("new max collatz length %d with index %d\n", max_length, max_length_index);
}
if(i % REPORT == 0)
{
printf("done with %d numbers\n", i);
}
}
printf("max collatz length %d with index %d\n", max_length, max_length_index);
}