-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
79 lines (63 loc) · 1.66 KB
/
graph.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <math.h>
#include <SDL2/SDL.h>
#define SCREEN_WIDTH 1920
#define SCREEN_HEIGHT 1080
unsigned __int128 lim = pow(2, 70);
unsigned __int128 n;
long count;
int cache[100000000];
int main() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("SDL_Init failed: %s\n", SDL_GetError());
return 1;
}
SDL_Window* window = SDL_CreateWindow("Collatz Conjecture", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
printf("SDL_CreateWindow failed: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL) {
printf("SDL_CreateRenderer failed: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Event event;
int quit = 0;
for (unsigned __int128 i = 1; i < lim && !quit; i++) {
n = i;
count = 0;
while (n != 1) {
count += 1;
if (n % 2 == 0) {
n /= 2;
} else {
n = 3 * n + 1;
}
if (cache[n] != 0) {
count += cache[n];
break;
}
}
cache[i] = count;
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderDrawPoint(renderer, i/10, count);
SDL_RenderPresent(renderer);
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = 1;
break;
}
}
if (quit) {
break;
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}