Length of rope tied around three equal circles touching each other

Given r is the radius of three equal circles touching each other. The task is to find the length of the rope tied around the circles as shown below:
Examples:Â
Input: r = 7Â
Output: 86Input: r = 14Â
Output: 172Â
Â
Approach: As it can be clearly seen from above image, the part of the length of rope which is not touching the circle is 2r + 2r + 2r = 6r.Â
The part of the rope which is touching the circles make a sector of 120 degrees on each circle. Thus, three sectors of 120 degrees each can be considered as a complete one circle of 360 degrees.Â
Therefore, Length of rope touching the circle is 2 * PI * r where PI = 22 / 7 and r is the radius of the circle.Â
Hence, the total length of the rope will be ( 2 * PI * r ) + 6r.
Below is the implementation of the above approach:Â Â
C++
// C++ program to find the length// of rope#include<bits/stdc++.h>using namespace std;#define PI 3.14159265Â
// Function to find the length// of ropefloat length_rope( float r ){Â Â Â Â return ( ( 2 * PI * r ) + 6 * r );}Â
// Driver codeint main(){Â Â Â Â float r = 7;Â Â Â Â cout<<ceil(length_rope( r ))<<endl;Â Â Â Â return 0;} |
C
// C program to find the length// of rope#include <stdio.h>#define PI 3.14159265Â
// Function to find the length// of ropefloat length_rope( float r ){Â Â Â Â return ( ( 2 * PI * r ) + 6 * r );}Â
// Driver codeint main(){    float r = 7;    printf("%f",           length_rope( r ));    return 0;} |
Java
// Java code to find the length// of ropeimport java.lang.*;Â
class GFG {Â
    static double PI = 3.14159265;Â
    // Function to find the length    // of rope    public static double length_rope(double r)    {        return ((2 * PI * r) + 6 * r);    }Â
    // Driver code    public static void main(String[] args)    {        double r = 7;        System.out.println(length_rope(r));    }} |
Python3
# Python3 code to find the length# of ropePI = 3.14159265Â Â Â Â Â # Function to find the length# of ropedef length_rope( r ):Â Â Â Â return ( ( 2 * PI * r ) + 6 * r )Â Â Â Â Â # Driver coder = 7print( length_rope( r )) |
C#
// C# code to find the length// of ropeusing System;Â
class GFG {Â Â Â Â static double PI = 3.14159265;Â
    // Function to find the length    // of rope    public static double length_rope(double r)    {        return ((2 * PI * r) + 6 * r);    }Â
    // Driver code    public static void Main()    {        double r = 7.0;        Console.Write(length_rope(r));    }} |
PHP
<?php// PHP program to find the // length of rope$PI = 3.14159265;Â
// Function to find the length// of ropefunction length_rope( $r ){Â Â Â Â global $PI;Â Â Â Â return ( ( 2 * $PI * $r ) + 6 * $r );}Â
// Driver code$r=7;echo(length_rope( $r ));?> |
Javascript
<script>Â
// Javascript program to find the length// of ropeconst PI = 3.14159265;Â
// Function to find the length// of ropefunction length_rope(r){Â Â Â Â return((2 * PI * r) + 6 * r);}Â
// Driver codelet r = 7;document.write(Math.ceil(length_rope(r)));Â
// This code is contributed by souravmahato348Â
</script> |
86
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




