-
Notifications
You must be signed in to change notification settings - Fork 2
/
collatz.rs
36 lines (32 loc) · 863 Bytes
/
collatz.rs
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
use graplot::{Desc, Plot, PlotArg};
fn main() {
let mut plot = Plot::default();
plot.set_title("Collatz Conjecture");
plot.set_desc(Desc {
min_steps_x: 10.,
spacing_x: 47.,
..Default::default()
});
for input in 1000..=1001 {
let mut single_graph = collatz(input as f64).as_plot();
single_graph.set_color(0., 1. * (input == 1000) as i32 as f32, 1.);
plot.add(single_graph);
}
plot.show();
}
fn collatz(input: f64) -> Vec<f64> {
let mut list: Vec<f64> = Vec::new();
if input != 0.0 {
let mut step: f64 = input;
while !list.contains(&step) {
list.push(step);
if step % 2.0 == 0.0 {
step /= 2.0;
} else {
step = step * 3.0 + 1.0
}
}
list.push(step);
}
list
}