Length of rope tied around three equal circles touching each other
Last Updated :
07 Jul, 2022
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: 86
Input: 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++
#include<bits/stdc++.h>
using namespace std;
#define PI 3.14159265
float length_rope( float r )
{
return ( ( 2 * PI * r ) + 6 * r );
}
int main()
{
float r = 7;
cout<< ceil (length_rope( r ))<<endl;
return 0;
}
|
C
#include <stdio.h>
#define PI 3.14159265
float length_rope( float r )
{
return ( ( 2 * PI * r ) + 6 * r );
}
int main()
{
float r = 7;
printf ( "%f" ,
length_rope( r ));
return 0;
}
|
Java
import java.lang.*;
class GFG {
static double PI = 3.14159265 ;
public static double length_rope( double r)
{
return (( 2 * PI * r) + 6 * r);
}
public static void main(String[] args)
{
double r = 7 ;
System.out.println(length_rope(r));
}
}
|
Python3
PI = 3.14159265
def length_rope( r ):
return ( ( 2 * PI * r ) + 6 * r )
r = 7
print ( length_rope( r ))
|
C#
using System;
class GFG {
static double PI = 3.14159265;
public static double length_rope( double r)
{
return ((2 * PI * r) + 6 * r);
}
public static void Main()
{
double r = 7.0;
Console.Write(length_rope(r));
}
}
|
PHP
<?php
$PI = 3.14159265;
function length_rope( $r )
{
global $PI ;
return ( ( 2 * $PI * $r ) + 6 * $r );
}
$r =7;
echo (length_rope( $r ));
?>
|
Javascript
<script>
const PI = 3.14159265;
function length_rope(r)
{
return ((2 * PI * r) + 6 * r);
}
let r = 7;
document.write(Math.ceil(length_rope(r)));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...