find the surface area of a regular polygon inscribed in a sphere in rust

To find the surface area of a regular polygon inscribed in a sphere, we can use the following formula:

A = n * a * r^2 * sin(2 * pi / n)

Where n is the number of sides of the polygon, a is the length of each side, and r is the radius of the sphere.

Here's the implementation of the above formula in Rust:

main.rs
use std::f64::consts::PI;

fn surface_area_of_polygon_inscribed_in_sphere(n: i32, a: f64, r: f64) -> f64 {
    n as f64 * a * r.powi(2) * (2.0 * PI / n as f64).sin()
}

fn main() {
    let n = 5;
    let a = 2.0;
    let r = 5.0;

    let area = surface_area_of_polygon_inscribed_in_sphere(n, a, r);

    println!("Surface area of the regular pentagon inscribed in sphere: {}", area);
}
387 chars
16 lines

In this implementation, we first define a function surface_area_of_polygon_inscribed_in_sphere that takes three arguments - n, a, and r - and returns the surface area of the polygon inscribed in the sphere.

Inside the function, we use Rust's built-in powi method to raise r to the power of 2.0, and sin method from the std::f64::consts::PI constant to calculate the sine of 2 * pi / n.

In the main function, we set the values of n, a, and r, and then call the surface_area_of_polygon_inscribed_in_sphere function to calculate the surface area of the polygon inscribed in the sphere. Finally, we print the result to the console.

gistlibby LogSnag