There are **4** circles with positive integer radius **r1**, **r2**, **r3** and **r4** as shown in the figure below.

The task is to find the radius **r4** of the circle formed by three circles when radius **r1**, **r2**, **r3** are given.

(Note that the circles in the picture above are tangent to each other.)

**Examples:**

Input:r1 = 1, r2 = 1, r3 = 1

Output:0.154701

Input:r1 = 23, r2 = 46, r3 = 69

Output:6.000000

**Approach 1: (Using Binary Search) : **

- The policy is to join the centers of all the circles and form 4 triangles
- After the triangles are formed, equate the sum of areas of the three smaller triangles with the main triangle as far as possible using binary search.

**Analysis of the mentioned approach: **

- This method works because initially there are 4 triangles as pointed in the above image.
- The main triangle with sides and the three smaller triangles with sides .
- The main triangle consists of the small ones so the area of the main triangle is the sum of the areas of the smaller ones.

**Forming a search space:**

Here binary search. The value for r can be chosen and the sum of the areas of all three smaller triangles can be computed and compared with the area of the main triangle.

- Choosing lower bound
- Choosing upper bound

By intuition, the upper bound value for r4 as the radius of the inscribed circle into the triangle is less than:

r_{upper_bound}

Now Binary Search can be applied at the following search space.

Below is the implementation of the problem using the above approach.

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Radius of the 3 given circles ` `// declared as double. ` `double` `r1, r2, r3; ` ` ` `// Calculation of area of a triangle by Heron's formula ` `double` `area(` `double` `a, ` `double` `b, ` `double` `c) ` `{ ` ` ` `double` `p = (a + b + c) / 2; ` ` ` `return` `sqrt` `(p) * ` `sqrt` `(p - a) * ` `sqrt` `(p - b) * ` `sqrt` `(p - c); ` `} ` ` ` `// Applying binary search to find the ` `// radius r4 of the required circle ` `double` `binary_search() ` `{ ` ` ` `// Area of main triangle ` ` ` `double` `s = area(r1 + r2, r2 + r3, r3 + r1); ` ` ` `double` `l = 0, h = s / (r1 + r2 + r3); ` ` ` `// Loop runs until l and h becomes approximately equal ` ` ` `while` `(h - l >= 1.e-7) { ` ` ` `double` `mid = (l + h) / 2; ` ` ` ` ` `// Area of smaller triangles ` ` ` `double` `s1 = area(mid + r1, mid + r2, r1 + r2); ` ` ` `double` `s2 = area(mid + r1, mid + r3, r1 + r3); ` ` ` `double` `s3 = area(mid + r2, mid + r3, r2 + r3); ` ` ` ` ` `// If sum of smaller triangles ` ` ` `// is less than main triangle ` ` ` `if` `(s1 + s2 + s3 < s) { ` ` ` `l = mid; ` ` ` `} ` ` ` `// If sum of smaller triangles is ` ` ` `// greater than or equal to main triangle ` ` ` `else` `{ ` ` ` `h = mid; ` ` ` `} ` ` ` `} ` ` ` `return` `(l + h) / 2; ` `} ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Taking r1, r2, r3 as input ` ` ` `r1 = 1.0; ` ` ` `r2 = 2.0; ` ` ` `r3 = 3.0; ` ` ` `// Call to function binary search ` ` ` `cout << fixed << setprecision(6) << binary_search() << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Radius of the 3 given circles ` ` ` `// declared as double. ` ` ` `static` `double` `r1, r2, r3; ` ` ` ` ` `// Calculation of area of a triangle by Heron's formula ` ` ` `static` `double` `area(` `double` `a, ` `double` `b, ` `double` `c) ` ` ` `{ ` ` ` `double` `p = (a + b + c) / ` `2` `; ` ` ` `return` `Math.sqrt(p) * Math.sqrt(p - a) * ` ` ` `Math.sqrt(p - b) * Math.sqrt(p - c); ` ` ` `} ` ` ` ` ` `// Applying binary search to find the ` ` ` `// radius r4 of the required circle ` ` ` `static` `double` `binary_search() ` ` ` `{ ` ` ` `// Area of main triangle ` ` ` `double` `s = area(r1 + r2, r2 + r3, r3 + r1); ` ` ` `double` `l = ` `0` `, h = s / (r1 + r2 + r3); ` ` ` ` ` `// Loop runs until l and h becomes approximately equal ` ` ` `while` `(h - l >= ` `1` `.e-` `7` `) ` ` ` `{ ` ` ` `double` `mid = (l + h) / ` `2` `; ` ` ` ` ` `// Area of smaller triangles ` ` ` `double` `s1 = area(mid + r1, mid + r2, r1 + r2); ` ` ` `double` `s2 = area(mid + r1, mid + r3, r1 + r3); ` ` ` `double` `s3 = area(mid + r2, mid + r3, r2 + r3); ` ` ` ` ` `// If sum of smaller triangles ` ` ` `// is less than main triangle ` ` ` `if` `(s1 + s2 + s3 < s) ` ` ` `{ ` ` ` `l = mid; ` ` ` `} ` ` ` ` ` `// If sum of smaller triangles is ` ` ` `// greater than or equal to main triangle ` ` ` `else` ` ` `{ ` ` ` `h = mid; ` ` ` `} ` ` ` `} ` ` ` `return` `(l + h) / ` `2` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Taking r1, r2, r3 as input ` ` ` `r1 = ` `1.0` `; ` ` ` `r2 = ` `2.0` `; ` ` ` `r3 = ` `3.0` `; ` ` ` ` ` `// Call to function binary search ` ` ` `System.out.printf(` `"%.6f"` `, binary_search()); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Radius of the 3 given circles ` ` ` `// declared as double. ` ` ` `static` `double` `r1, r2, r3; ` ` ` ` ` `// Calculation of area of a triangle by Heron's formula ` ` ` `static` `double` `area(` `double` `a, ` `double` `b, ` `double` `c) ` ` ` `{ ` ` ` `double` `p = (a + b + c) / 2; ` ` ` `return` `Math.Sqrt(p) * Math.Sqrt(p - a) * ` ` ` `Math.Sqrt(p - b) * Math.Sqrt(p - c); ` ` ` `} ` ` ` ` ` `// Applying binary search to find the ` ` ` `// radius r4 of the required circle ` ` ` `static` `double` `binary_search() ` ` ` `{ ` ` ` `// Area of main triangle ` ` ` `double` `s = area(r1 + r2, r2 + r3, r3 + r1); ` ` ` `double` `l = 0, h = s / (r1 + r2 + r3); ` ` ` ` ` `// Loop runs until l and h ` ` ` `// becomes approximately equal ` ` ` `while` `(h - l > 0.00000001) ` ` ` `{ ` ` ` `double` `mid = (l + h) / 2; ` ` ` ` ` `// Area of smaller triangles ` ` ` `double` `s1 = area(mid + r1, mid + r2, r1 + r2); ` ` ` `double` `s2 = area(mid + r1, mid + r3, r1 + r3); ` ` ` `double` `s3 = area(mid + r2, mid + r3, r2 + r3); ` ` ` ` ` `// If sum of smaller triangles ` ` ` `// is less than main triangle ` ` ` `if` `(s1 + s2 + s3 < s) ` ` ` `{ ` ` ` `l = mid; ` ` ` `} ` ` ` ` ` `// If sum of smaller triangles is ` ` ` `// greater than or equal to main triangle ` ` ` `else` ` ` `{ ` ` ` `h = mid; ` ` ` `} ` ` ` `} ` ` ` `return` `(l + h) / 2; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `// Taking r1, r2, r3 as input ` ` ` `r1 = 1.0; ` ` ` `r2 = 2.0; ` ` ` `r3 = 3.0; ` ` ` ` ` `// Call to function binary search ` ` ` `Console.Write(` `"{0:F6}"` `, binary_search()); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

0.260870

**Approach 2: (Using Descartes’ Theorem) **

- According to Descartes’ Theorem, the reciprocals of radii, or “curvatures”, of these circles satisfy the following relation.
- If are known, one can solve for,
- On solving the above equation;

Below is the implementation of the problem using the above formula.

## CPP

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Radius of the 3 given circles declared as double. ` ` ` `double` `r1, r2, r3; ` ` ` ` ` `// Taking r1, r2, r3 as input ` ` ` `r1 = 1; ` ` ` `r2 = 2; ` ` ` `r3 = 3; ` ` ` ` ` `// Calculation of r4 using formula given above ` ` ` `double` `r4 = (r1 * r2 * r3) ` ` ` `/ (r1 * r2 + r2 * r3 + r1 * r3 ` ` ` `+ 2.0 * ` `sqrt` `(r1 * r2 * r3 * (r1 + r2 + r3))); ` ` ` `cout << fixed << setprecision(6) << r4 << ` `'\n'` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// Radius of the 3 given circles declared as double. ` ` ` `double` `r1, r2, r3; ` ` ` ` ` `// Taking r1, r2, r3 as input ` ` ` `r1 = ` `1` `; ` ` ` `r2 = ` `2` `; ` ` ` `r3 = ` `3` `; ` ` ` ` ` `// Calculation of r4 using formula given above ` ` ` `double` `r4 = (r1 * r2 * r3) / ` ` ` `(r1 * r2 + r2 * r3 + r1 * r3 + ` `2.0` `* ` ` ` `Math.sqrt(r1 * r2 * r3 * (r1 + r2 + r3))); ` ` ` `System.out.printf(` `"%.6f"` `, r4); ` ` ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python

`# Python3 implementation of the approach ` `from` `math ` `import` `sqrt ` ` ` `# Driver code ` ` ` `# Radius of the 3 given circles declared as double. ` ` ` `# Taking r1, r2, r3 as input ` `r1 ` `=` `1` `r2 ` `=` `2` `r3 ` `=` `3` ` ` `# Calculation of r4 using formula given above ` `r4 ` `=` `(r1 ` `*` `r2 ` `*` `r3)` `/` `(r1 ` `*` `r2 ` `+` `r2 ` `*` `r3 ` `+` `r1 ` `*` `r3 ` ` ` `+` `2.0` `*` `sqrt(r1 ` `*` `r2 ` `*` `r3 ` `*` `(r1 ` `+` `r2 ` `+` `r3))) ` `print` `(` `round` `(r4, ` `6` `)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `// Radius of the 3 given circles declared as double. ` ` ` `double` `r1, r2, r3; ` ` ` ` ` `// Taking r1, r2, r3 as input ` ` ` `r1 = 1; ` ` ` `r2 = 2; ` ` ` `r3 = 3; ` ` ` ` ` `// Calculation of r4 using formula given above ` ` ` `double` `r4 = (r1 * r2 * r3) / ` ` ` `(r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * ` ` ` `Math.Sqrt(r1 * r2 * r3 * (r1 + r2 + r3))); ` ` ` `Console.Write(` `"{0:F6}"` `, r4); ` ` ` `} ` `} ` ` ` `// This code contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

0.260870

**Reference** :

- https://brilliant.org/wiki/descartes-theorem/
- https://en.wikipedia.org/wiki/Descartes%27_theorem/
- http://www.ambrsoft.com/TrigoCalc/Circles3/Tangency/Tangent.htm

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Radii of the three tangent circles of equal radius which are inscribed within a circle of given radius
- Radius of the biggest possible circle inscribed in rhombus which in turn is inscribed in a rectangle
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Circle
- Area of Equilateral triangle inscribed in a Circle of radius R
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Length of the transverse common tangent between the two non intersecting circles
- Length of the direct common tangent between two externally touching circles
- Length of direct common tangent between the two non-intersecting Circles
- Length of direct common tangent between two intersecting Circles
- Area of a square inscribed in a circle which is inscribed in an equilateral triangle
- Area of a circle inscribed in a rectangle which is inscribed in a semicircle
- Area of a square inscribed in a circle which is inscribed in a hexagon

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.