collatz/
collatz.rs

1use graplot::{Desc, Plot, PlotArg};
2
3fn main() {
4    let mut plot = Plot::default();
5    plot.set_title("Collatz Conjecture");
6    plot.set_desc(Desc {
7        min_steps_x: 10.,
8        spacing_x: 47.,
9        ..Default::default()
10    });
11
12    for input in 1000..=1001 {
13        let mut single_graph = collatz(input as f64).as_plot();
14        single_graph.set_color(0., 1. * (input == 1000) as i32 as f32, 1.);
15        plot.add(single_graph);
16    }
17    plot.show();
18}
19
20fn collatz(input: f64) -> Vec<f64> {
21    let mut list: Vec<f64> = Vec::new();
22    if input != 0.0 {
23        let mut step: f64 = input;
24
25        while !list.contains(&step) {
26            list.push(step);
27            if step % 2.0 == 0.0 {
28                step /= 2.0;
29            } else {
30                step = step * 3.0 + 1.0
31            }
32        }
33        list.push(step);
34    }
35    list
36}