Given two integers r and R representing the length of Inradius and Circumradius respectively, the task is to calculate the distance d between Incenter and Circumcenter.
Inradius The inradius( r ) of a regular triangle( ABC ) is the radius of the incircle (having center as l), which is the largest circle that will fit inside the triangle.Â
Circumradius: The circumradius( R ) of a triangle is the radius of the circumscribed circle (having center as O) of that triangle.Â
Examples:Â
Input: r = 2, R = 5Â
Output: 2.24Input: r = 5, R = 12Â
Output: 4.9
Approach:Â
The problem can be solved using Euler’s Theorem in geometry, which states that the distance between the incenter and circumcenter of a triangle can be calculated by the equation:
Below is the implementation of the above approach:
C++14
// C++14 program for the above approach#include <bits/stdc++.h> using namespace std;Â
// Function returns the required distancedouble distance(int r, int R) { Â Â Â Â double d = sqrt(pow(R, 2) - Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (2 * r * R)); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return d; } Â
// Driver code int main() {          // Length of Inradius     int r = 2;          // Length of Circumradius     int R = 5; Â
    cout << (round(distance(r, R) * 100.0) / 100.0); } Â
// This code is contributed by sanjoy_62 |
Java
// Java program for the above approach import java.util.*;Â
class GFG{Â Â Â Â Â // Function returns the required distancestatic double distance(int r,int R){Â Â Â Â double d = Math.sqrt(Math.pow(R, 2) -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (2 * r * R));Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return d;}Â
// Driver codepublic static void main(String[] args){         // Length of Inradius    int r = 2;         // Length of Circumradius    int R = 5;Â
    System.out.println(Math.round(        distance(r, R) * 100.0) / 100.0);}}Â
// This code is contributed by offbeat |
Python3
# Python3 program for the above approachimport mathÂ
# Function returns the required distancedef distance(r,R):Â
    d = math.sqrt( (R**2) - (2 * r * R))         return dÂ
# Driver CodeÂ
# Length of Inradiusr = 2Â
# Length of CircumradiusR = 5Â
print(round(distance(r,R),2)) |
C#
// C# program for the above approach using System;Â
class GFG{Â Â Â Â Â // Function returns the required distancestatic double distance(int r, int R){Â Â Â Â double d = Math.Sqrt(Math.Pow(R, 2) -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (2 * r * R));Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return d;}Â
// Driver codepublic static void Main(string[] args){         // Length of Inradius    int r = 2;         // Length of Circumradius    int R = 5;         Console.Write(Math.Round(        distance(r, R) * 100.0) / 100.0);}}Â
// This code is contributed by rutvik_56 |
Javascript
<script>Â
// Javascript program for// the above approachÂ
// Function returns the required distancefunction distance(r, R){Â Â Â Â let d = Math.sqrt(Math.pow(R, 2) -Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â (2 * r * R));Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â return d;}Â
// Driver codeÂ
    // Length of Inradius    let r = 2;           // Length of Circumradius    let R = 5;       document.write(Math.round(        distance(r, R) * 100.0) / 100.0);           // This code is contributed by susmitakundugoaldanga.</script> |
2.24
Time Complexity: O(logn) since time complexity of sqrt is O(logn)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

