-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06-creative.R
107 lines (93 loc) · 2.46 KB
/
06-creative.R
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
library(tidyverse)
library(hexbin)
# Code for Task 6 Creative Visualization ---------------------------------------
# 1) Plot the highest value reached by each starting integer -------------------
# y-axis(0,100000)
collatz_df %>%
unnest(seq) %>%
group_by(start) %>%
filter(start %in% 1:10000) %>%
slice_max(order_by = seq) %>%
ggplot(.,
aes(x = start,
y = seq)) +
geom_point(aes(col = start,
alpha = length),
size = 1) +
labs(
title = "Collatz Conjecture",
subtitle = "Max value reached by each starting integer ",
x = "Starting Integer",
y = "Value"
) +
theme_minimal() +
xlim(0, 10000) +
ylim(0, 100000)
ggsave("highest_value_reached_by_each_starting_integer.png",
width = 1980,
height = 1980,
units = "px",
bg = "white",
dpi = 300)
# 2) Numerical Progression for each starting integer ---------------------------
# Let's look at the numerical progression of starting integer n = 27
collatz_df %>%
unnest(seq) %>%
group_by(start) %>%
filter(start %in% 27) %>%
mutate(steps = row_number()) %>%
ggplot(.,
aes(x = steps,
y = seq)) +
geom_line() +
labs(
title = "Collatz Sequence Line Plot",
x = "Steps",
y = "Value"
) +
theme_classic()
ggsave("numerical_progression_of_27.png",
width = 1980,
height = 1980,
units = "px",
bg = "white",
dpi = 300)
# Now, for starting integer from 1:30
collatz_df %>%
unnest(seq) %>%
group_by(start) %>%
filter(start %in% 1:30) %>%
mutate(steps = row_number()) %>%
ggplot(.,
aes(x = steps,
y = seq)) +
geom_line(aes(col = length)) +
facet_wrap(start ~ length, scales = "free") +
labs(
title = "Collatz Sequence Line Plot",
subtitle = "Numerical Progression for each starting integer",
x = "Steps",
y = "Value"
) +
theme_classic()
ggsave("numerical_progression_of_1_to_30.png",
width = 2500,
height = 2500,
units = "px",
bg = "white",
dpi = 300)
# 3) How long are Collatz sequences? -------------------------------------------
collatz_df %>%
ggplot(.,
aes(x = start,
y = length)) +
geom_hex(bins = 20) +
scale_y_continuous(breaks = seq(0, 275, by = 25)) +
scale_fill_viridis_c() +
theme_minimal()
ggsave("collatz_sequences_hex.png",
width = 2500,
height = 2500,
units = "px",
bg = "white",
dpi = 300)