Yellowstone sequence: Difference between revisions
Content added Content deleted
m (Minor edit to C++ code) |
(Rust solution extended to plot the first 100 numbers in the sequence) |
||
Line 1,150: | Line 1,150: | ||
<lang rust>// [dependencies] |
<lang rust>// [dependencies] |
||
// num = "0.3" |
// num = "0.3" |
||
// plotters = "^0.2.15" |
|||
use num::integer::gcd; |
use num::integer::gcd; |
||
use std::collections::HashSet; |
use std::collections::HashSet; |
||
use plotters::prelude::*; |
|||
fn yellowstone_sequence() -> impl std::iter::Iterator<Item = u32> { |
fn yellowstone_sequence() -> impl std::iter::Iterator<Item = u32> { |
||
Line 1,158: | Line 1,160: | ||
let mut min = 1; |
let mut min = 1; |
||
let mut n = 0; |
let mut n = 0; |
||
let mut n1 = |
let mut n1 = 0; |
||
let mut n2 = 0; |
let mut n2 = 0; |
||
std::iter::from_fn(move || { |
std::iter::from_fn(move || { |
||
Line 1,178: | Line 1,180: | ||
Some(n) |
Some(n) |
||
}) |
}) |
||
} |
|||
// Based on the example in the "Quick Start" section of the README file for |
|||
// the plotters library. |
|||
fn plot_yellowstone(filename: &str) -> Result<(), Box<dyn std::error::Error>> { |
|||
let root = BitMapBackend::new(filename, (800, 600)).into_drawing_area(); |
|||
root.fill(&WHITE)?; |
|||
let mut chart = ChartBuilder::on(&root) |
|||
.caption("Yellowstone Sequence", ("sans-serif", 24).into_font()) |
|||
.margin(10) |
|||
.x_label_area_size(20) |
|||
.y_label_area_size(20) |
|||
.build_ranged(0usize..100usize, 0u32..180u32)?; |
|||
chart.configure_mesh().draw()?; |
|||
chart.draw_series(LineSeries::new( |
|||
yellowstone_sequence().take(100).enumerate(), |
|||
&BLUE))?; |
|||
Ok(()) |
|||
} |
} |
||
Line 1,186: | Line 1,206: | ||
} |
} |
||
println!(); |
println!(); |
||
match plot_yellowstone("yellowstone.png") { |
|||
Ok(()) => {} |
|||
Err(error) => eprintln!("Error: {}", error) |
|||
} |
|||
}</lang> |
}</lang> |
||
{{out}} |
{{out}} |
||
A plot of the first 100 Yellowstone numbers is saved to the file "yellowstone.png". |
|||
<pre> |
<pre> |
||
First 30 Yellowstone numbers: |
First 30 Yellowstone numbers: |