Find N Geometric Means between A and B

Given three integers A, B and N the task is to find N Geometric means between A and B. WE basically need to insert N terms in a Geometric progression. where A and B are first and last terms.
Examples:

Input :  A = 2  B = 32  N = 3
Output : 4 8 16
the geometric progression series as 2,
4, 8, 16 , 32
       
Input : A = 3 B = 81 N = 2
Output : 9 27


Approach :
Let A1, G2, G3, G4……Gn be N geometric Means between two given numbers A and B . Then A, G1, G2 ….. Gn, B will be in Geometric Progression .

So B = (N+2)th term of the Geometric progression.

Then Here R is the common ratio
B = A*RN+1
RN+1 = B/A
R = (B/A)1/(N+1)

Now we have the value of R
And also we have the value of the first term A
G1 = AR1 = A * (B/A)1/(N+1)
G2 = AR2 = A * (B/A)2/(N+1)
G3 = AR3 = A * (B/A)3/(N+1)
.
.
.
GN = ARN = A * (B/A)N/(N+1)

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find n geometric means
// between A and B
#include <bits/stdc++.h>
using namespace std;
  
// Prints N geometric means between
// A and B.
void printGMeans(int A, int B, int N)
{
    // calculate common ratio(R)
    float R = (float)pow(float(B / A), 
                  1.0 / (float)(N + 1));
      
    // for finding N the Geometric 
    // mean between A and B
    for (int i = 1; i <= N; i++) 
        cout << A * pow(R, i) <<" ";    
}
  
// Driver code to test above 
int main()
{
    int A = 3, B = 81, N = 2;
    printGMeans(A, B, N);    
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program to ilustrate
// n geometric mean between 
// A and B
import java.io.*;
import java.lang.*;
import java.util.*;
  
public class GFG {
  
    // insert function for calulating the means
    static void printGMeans(int A, int B, int N)
    {       
        // Finding the value of R Common ration
        float R = (float)Math.pow((float)(B / A),
                           1.0 / (float)(N + 1));
                             
        // for finding N the Geometric 
        // mean between A and B
        for (int i = 1; i <= N; i++) 
          System.out.print(A * Math.pow(R, i) + " ");
          
    }
  
    // Driver code
    public static void main(String args[])
    {
        int A = 3, B = 81, N = 2;
        printGMeans(A, B, N);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find
# n geometric means
# between A and B
import math
  
# Prints N geometric means 
# between A and B.
def printGMeans(A, B, N):
      
    # calculate
    # common ratio(R)
    R = (math.pow((B / A),
          1.0 / (N + 1)));
      
    # for finding N the 
    # Geometric mean 
    # between A and B
    for i in range(1, N + 1):
        print(int(A * math.pow(R, i)), 
                           end = " "); 
  
# Driver Code
A = 3;
B = 81;
N = 2;
printGMeans(A, B, N); 
      
# This code is contributed
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to ilustrate
// n geometric mean between 
// A and B
using System;
  
public class GFG {
  
    // insert function for calulating the means
    static void printGMeans(int A, int B, int N)
    
          
        // Finding the value of R Common ration
        float R = (float)Math.Pow((float)(B / A),
                        1.0 / (float)(N + 1));
                              
        // for finding N the Geometric 
        // mean between A and B
        for (int i = 1; i <= N; i++) 
            Console.Write(A * Math.Pow(R, i) + " ");
          
    }
  
    // Driver code
    public static void Main()
    {
        int A = 3, B = 81, N = 2;
          
        printGMeans(A, B, N);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find
// n geometric means
// between A and B
  
// Pr$s N geometric means 
// between A and B.
function printGMeans($A, $B, $N)
{
      
    // calculate common ratio(R)
    $R = pow(($B / $A), 
             1.0 / ($N + 1));
      
    // for finding N the Geometric 
    // mean between A and B
    for ($i = 1; $i <= $N; $i++) 
        echo $A * pow($R, $i) ," "
}
  
    // Driver Code
    $A = 3;
    $B = 81;
    $N = 2;
    printGMeans($A, $B, $N); 
      
// This code is contributed by anuj_67.
?>

chevron_right



Output :

9 27 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : vt_m, Mithun Kumar