How to resolve the algorithm Goldbach's comet step by step in the Rust programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Goldbach's comet step by step in the Rust programming language

Table of Contents

Problem Statement

Goldbach's comet is the name given to a plot of the function g(E), the so-called Goldbach function. The Goldbach function is studied in relation to Goldbach's conjecture. The function g(E) is defined for all even integers E>2 to be the number of different ways in which E can be expressed as the sum of two primes.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Goldbach's comet step by step in the Rust programming language

Source code in the rust programming language

// [dependencies]
// primal = "0.3"
// plotters = "0.3.2"

use plotters::prelude::*;

fn goldbach(n: u64) -> u64 {
    let mut p = 2;
    let mut count = 0;
    loop {
        let q = n - p;
        if q < p {
            break;
        }
        if primal::is_prime(p) && primal::is_prime(q) {
            count += 1;
        }
        if p == 2 {
            p += 1;
        } else {
            p += 2;
        }
    }
    count
}

fn goldbach_plot(filename: &str) -> Result<(), Box<dyn std::error::Error>> {
    let gvalues : Vec<u64> = (1..=2000).map(|x| goldbach(2 * x + 2)).collect();
    let mut gmax = *gvalues.iter().max().unwrap();
    gmax = 10 * ((gmax + 9) / 10);

    let root = SVGBackend::new(filename, (1000, 500)).into_drawing_area();
    root.fill(&WHITE)?;

    let mut chart = ChartBuilder::on(&root)
        .x_label_area_size(20)
        .y_label_area_size(20)
        .margin(10)
        .caption("Goldbach's Comet", ("sans-serif", 24).into_font())
        .build_cartesian_2d(0usize..2000usize, 0u64..gmax)?;

    chart
        .configure_mesh()
        .disable_x_mesh()
        .disable_y_mesh()
        .draw()?;

    chart.draw_series(
        gvalues
            .iter()
            .cloned()
            .enumerate()
            .map(|p| Circle::new(p, 2, BLUE.filled())),
    )?;

    Ok(())
}

fn main() {
    println!("First 100 G numbers:");
    for i in 1..=100 {
        print!(
            "{:2}{}",
            goldbach(2 * i + 2),
            if i % 10 == 0 { "\n" } else { " " }
        );
    }

    println!("\nG(1000000) = {}", goldbach(1000000));

    match goldbach_plot("goldbach.svg") {
        Ok(()) => {}
        Err(error) => eprintln!("Error: {}", error),
    }
}


  

You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Scheme programming language
You may also check:How to resolve the algorithm String matching step by step in the Ring programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the Lua programming language
You may also check:How to resolve the algorithm P-Adic square roots step by step in the Julia programming language
You may also check:How to resolve the algorithm Generate random chess position step by step in the Ruby programming language