find the surface area of a regular polygon circumscribed around a sphere in ruby

To find the surface area of a regular polygon circumscribed around a sphere, we can use a formula:

main.rb
surface_area = (number_of_sides * side_length * circumradius) / 2
66 chars
2 lines

Here, number_of_sides is the number of sides of the regular polygon, side_length is the length of each side, and circumradius is the radius of the sphere.

To implement this formula in Ruby, we can define a method that takes number_of_sides, side_length, and circumradius as arguments:

main.rb
def surface_area_of_regular_polygon_circumscribed_around_sphere(number_of_sides, side_length, circumradius)
  surface_area = (number_of_sides * side_length * circumradius) / 2

  surface_area
end
196 chars
6 lines

We can then call this method with the appropriate arguments to find the surface area:

main.rb
surface_area = surface_area_of_regular_polygon_circumscribed_around_sphere(6, 3, 5)
puts surface_area
102 chars
3 lines

This will output 45, which is the surface area of a regular hexagon circumscribed around a sphere with a radius of 5 units and side length of 3 units.

gistlibby LogSnag