-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3NPlus1.c
More file actions
68 lines (58 loc) · 1.74 KB
/
3NPlus1.c
File metadata and controls
68 lines (58 loc) · 1.74 KB
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
66
67
68
// 3n + 1 algorithm by @Caisesiume
#include <stdio.h>
typedef struct {
int length;
int highestValue;
} SequenceInfo;
SequenceInfo collatzSequence(int n) {
SequenceInfo info;
info.length = 1;
info.highestValue = n;
printf("%d: ", n);
while (n != 1) {
printf("%d, ", n);
if (n % 2 == 0) {
n /= 2;
} else {
n = 3 * n + 1;
}
if (n > info.highestValue) {
info.highestValue = n;
}
info.length++;
}
printf("1\n");
return info;
}
int main() {
int upperLimit = 50;
int highestNumber = 0;
int highestNumberIteration = 0;
int overallHighest = 0;
int longest = 0;
int longestIteration = 0;
int totalVisited = 0;
int totalSequences = 0;
for (int i = 1; i <= upperLimit; i++) {
SequenceInfo sequenceInfo = collatzSequence(i);
totalVisited += sequenceInfo.length;
totalSequences++;
if (i > highestNumber) {
highestNumber = i;
}
if (sequenceInfo.length > longest) {
longest = sequenceInfo.length;
longestIteration = i;
}
if (sequenceInfo.highestValue > overallHighest) {
overallHighest = sequenceInfo.highestValue;
highestNumberIteration = i;
}
}
printf("\n-----STATS AFTER CALCULATIONS-----\n");
printf("Overall highest integer reached: %d, reached in iteration %d\n", overallHighest, highestNumberIteration);
printf("Longest sequence: %d nodes, occurred in iteration: %d\n", longest - 1, longestIteration);
printf("Total numbers (nodes) visited: %d\n", totalVisited);
printf("Amount of numbers (sequences) calculated: %d\n", totalSequences);
return 0;
}