-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[SOLVED] Problem 14 (finding longest collatz string).c
65 lines (49 loc) · 1.4 KB
/
[SOLVED] Problem 14 (finding longest collatz string).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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//0. INFORMATION
//Author: Thomas Hollis
//1. INCLUSIONS
#include <stdio.h>
#include <stdlib.h>
//#include <string.h>
#include <math.h>
#include <inttypes.h>
//#include <complex.h>
//#include <ctype.h>
//#include <gsl/gsl_linalg.h>
//#include <stdbool.h>
//2. DEFINITIONS
//#define PI 3.1415926
//#define E 2.71828182846
//#define E0 0.00000000000885
//3. FUNCTION PROTOTYPES
//4. MAIN
int main(void) {
//variable declaration
uint64_t maxChain = 0, chain = 0, collatz = 0, maxCollatz = 0, i = 1;
//processing solution
while(i < 1000000) {
chain = 1;
collatz = i;
while(collatz != 1) {
if(collatz % 2 != 0) {
collatz = (3 * collatz) + 1;
chain++;
}
while(collatz % 2 == 0) {
collatz /= 2;
chain++;
}
}
if(chain > maxChain)
{
maxChain = chain;
maxCollatz = i;
}
i++;
}
//print output
printf("\n Sequence length: %"PRId64" \n Starting number: %"PRId64" \n", maxChain, maxCollatz);
return 0;
}
//5. FUNCTIONS
//6. DEBUG STATUS
//!debugged