# Find the missing number in geometric progression

Here given code implementation process.

``````// C program for
// Find the missing number in geometric progression
#include <stdio.h>
#include <math.h>

int missingNo(int series[], int low, int high, int ratio)
{
if (low > high)
{
// When no result
return -1;
}
// Find middle element
int mid = low + (high - low) / 2;
if (mid + 1 <= high && (series[mid + 1] / series[mid]) != ratio)
{
return series[mid] *ratio;
}
if (mid > 0 && (series[mid] / series[mid - 1]) != ratio)
{
return series[mid - 1] *ratio;
}
if ((int) pow(ratio, mid) *series[0] == series[mid])
{
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return missingNo(series, mid + 1, high, ratio);
}
// Check sequence of left-sided elements from middle.
return missingNo(series, low, mid - 1, ratio);
}
void missingGeometricPro(int series[], int n)
{
if (n > 1)
{
// When n is more than 1
// Find ratio between sequence
int ratio = (int) pow(series[n - 1] / series[0], 1.0 / n);
// Find missing element
int value = missingNo(series, 0, n - 1, ratio);
// Display missing element
printf("\n %d", value);
}
}
int main(int argc, char
const *argv[])
{
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
int a[] = {
1 , 5 , 25 , 625 , 3125
};
int b[] = {
6 , 12 , 24 , 48 , 96 , 192 , 768 , 1536
};
int c[] = {
5 , 20 , 40 , 80 , 160
};
// Formula : x, xr, xr^2, xr^3...
// Test A
int n = sizeof(a) / sizeof(a[0]);
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
missingGeometricPro(a, n);
// Test B
n = sizeof(b) / sizeof(b[0]);
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
missingGeometricPro(b, n);
// Test C
n = sizeof(c) / sizeof(c[0]);
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
missingGeometricPro(c, n);
return 0;
}``````

#### Output

`````` 125
384
10``````
``````/*
Java program
Find the missing number in geometric progression
*/
public class GeometricProgression
{
public int missingNo(int[] series, int low, int high, int ratio)
{
if (low > high)
{
// When no result
return -1;
}
// Find middle element
int mid = low + (high - low) / 2;
if (mid + 1 <= high && (series[mid + 1] / series[mid]) != ratio)
{
return series[mid] * ratio;
}
if (mid > 0 && (series[mid] / series[mid - 1]) != ratio)
{
return series[mid - 1] * ratio;
}
if ((int) Math.pow(ratio, mid) * series[0] == series[mid])
{
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return missingNo(series, mid + 1, high, ratio);
}
// Check sequence of left-sided elements from middle.
return missingNo(series, low, mid - 1, ratio);
}
public void missingGeometricPro(int[] series, int n)
{
if (n > 1)
{
// When n is more than 1
// Find ratio between sequence
int ratio = (int) Math.pow(series[n - 1] / series[0], 1.0 / n);
// Find missing element
int value = missingNo(series, 0, n - 1, ratio);
// Display missing element
System.out.print("\n " + value);
}
}
public static void main(String[] args)
{
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
int[] a = {
1 , 5 , 25 , 625 , 3125
};
int[] b = {
6 , 12 , 24 , 48 , 96 , 192 , 768 , 1536
};
int[] c = {
5 , 20 , 40 , 80 , 160
};
// Formula : x, xr, xr^2, xr^3...
// Test A
int n = a.length;
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
// Test B
n = b.length;
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
// Test C
n = c.length;
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
}
}``````

#### Output

`````` 125
384
10``````
``````// Include header file
#include <iostream>
#include <math.h>

using namespace std;
/*
C++ program
Find the missing number in geometric progression
*/
class GeometricProgression
{
public: int missingNo(int series[], int low, int high, int ratio)
{
if (low > high)
{
// When no result
return -1;
}
// Find middle element
int mid = low + (high - low) / 2;
if (mid + 1 <= high && (series[mid + 1] / series[mid]) != ratio)
{
return series[mid] *ratio;
}
if (mid > 0 && (series[mid] / series[mid - 1]) != ratio)
{
return series[mid - 1] *ratio;
}
if ((int) pow(ratio, mid) *series[0] == series[mid])
{
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return this->missingNo(series, mid + 1, high, ratio);
}
// Check sequence of left-sided elements from middle.
return this->missingNo(series, low, mid - 1, ratio);
}
void missingGeometricPro(int series[], int n)
{
if (n > 1)
{
// When n is more than 1
// Find ratio between sequence
int ratio = (int) pow(series[n - 1] / series[0], 1.0 / n);
// Find missing element
int value = this->missingNo(series, 0, n - 1, ratio);
// Display missing element
cout << "\n " << value;
}
}
};
int main()
{
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
int a[] = {
1 , 5 , 25 , 625 , 3125
};
int b[] = {
6 , 12 , 24 , 48 , 96 , 192 , 768 , 1536
};
int c[] = {
5 , 20 , 40 , 80 , 160
};
// Formula : x, xr, xr^2, xr^3...
// Test A
int n = sizeof(a) / sizeof(a[0]);
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
// Test B
n = sizeof(b) / sizeof(b[0]);
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
// Test C
n = sizeof(c) / sizeof(c[0]);
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
return 0;
}``````

#### Output

`````` 125
384
10``````
``````// Include namespace system
using System;
/*
Csharp program
Find the missing number in geometric progression
*/
public class GeometricProgression
{
public int missingNo(int[] series, int low, int high, int ratio)
{
if (low > high)
{
// When no result
return -1;
}
// Find middle element
int mid = low + (high - low) / 2;
if (mid + 1 <= high && (series[mid + 1] / series[mid]) != ratio)
{
return series[mid] * ratio;
}
if (mid > 0 && (series[mid] / series[mid - 1]) != ratio)
{
return series[mid - 1] * ratio;
}
if ((int) Math.Pow(ratio, mid) * series[0] == series[mid])
{
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return this.missingNo(series, mid + 1, high, ratio);
}
// Check sequence of left-sided elements from middle.
return this.missingNo(series, low, mid - 1, ratio);
}
public void missingGeometricPro(int[] series, int n)
{
if (n > 1)
{
// When n is more than 1
// Find ratio between sequence
int ratio = (int) Math.Pow(series[n - 1] / series[0], 1.0 / n);
// Find missing element
int value = this.missingNo(series, 0, n - 1, ratio);
// Display missing element
Console.Write("\n " + value);
}
}
public static void Main(String[] args)
{
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
int[] a = {
1 , 5 , 25 , 625 , 3125
};
int[] b = {
6 , 12 , 24 , 48 , 96 , 192 , 768 , 1536
};
int[] c = {
5 , 20 , 40 , 80 , 160
};
// Formula : x, xr, xr^2, xr^3...
// Test A
int n = a.Length;
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
// Test B
n = b.Length;
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
// Test C
n = c.Length;
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
}
}``````

#### Output

`````` 125
384
10``````
``````package main
import "math"
import "fmt"
/*
Go program
Find the missing number in geometric progression
*/
type GeometricProgression struct {}
func getGeometricProgression() * GeometricProgression {
var me *GeometricProgression = &GeometricProgression {}
return me
}
func(this GeometricProgression) missingNo(series[] int,
low int, high int, ratio int) int {
if low > high {
// When no result
return -1
}
// Find middle element
var mid int = low + (high - low) / 2
if mid + 1 <= high && (series[mid + 1] / series[mid]) != ratio {
return series[mid] * ratio
}
if mid > 0 && (series[mid] / series[mid - 1]) != ratio {
return series[mid - 1] * ratio
}
if int(math.Pow(float64(ratio), float64(mid))) * series[0] == series[mid] {
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return this.missingNo(series, mid + 1, high, ratio)
}
// Check sequence of left-sided elements from middle.
return this.missingNo(series, low, mid - 1, ratio)
}
func(this GeometricProgression) missingGeometricPro(series[] int, n int) {
if n > 1 {
// When n is more than 1
// Find ratio between sequence
var ratio int = int(math.Pow(float64(series[n - 1]) / float64(series[0]),
1.0 / float64(n)))
// Find missing element
var value int = this.missingNo(series, 0, n - 1, ratio)
// Display missing element
fmt.Print("\n ", value)
}
}
func main() {
var task * GeometricProgression = getGeometricProgression()
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
var a = [] int {
1,
5,
25,
625,
3125,
}
var b = [] int {
6,
12,
24,
48,
96,
192,
768,
1536,
}
var c = [] int {
5,
20,
40,
80,
160,
}
// Formula : x, xr, xr^2, xr^3...
// Test A
var n int = len(a)
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
// Test B
n = len(b)
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
// Test C
n = len(c)
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
}``````

#### Output

`````` 125
384
10``````
``````<?php
/*
Php program
Find the missing number in geometric progression
*/
class GeometricProgression
{
public	function missingNo(\$series, \$low, \$high, \$ratio)
{
if (\$low > \$high)
{
// When no result
return -1;
}
// Find middle element
\$mid = \$low + (int)((\$high - \$low) / 2);
if (\$mid + 1 <= \$high &&
((int)(\$series[\$mid + 1] / \$series[\$mid])) != \$ratio)
{
return \$series[\$mid] * \$ratio;
}
if (\$mid > 0 &&
((int)(\$series[\$mid] / \$series[\$mid - 1])) != \$ratio)
{
return \$series[\$mid - 1] * \$ratio;
}
if ((int) pow(\$ratio, \$mid) * \$series[0] == \$series[\$mid])
{
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return \$this->missingNo(\$series, \$mid + 1, \$high, \$ratio);
}
// Check sequence of left-sided elements from middle.
return \$this->missingNo(\$series, \$low, \$mid - 1, \$ratio);
}
public	function missingGeometricPro(\$series, \$n)
{
if (\$n > 1)
{
// When n is more than 1
// Find ratio between sequence
\$ratio = (int) pow((int)(\$series[\$n - 1] / \$series[0]), 1.0 / \$n);
// Find missing element
\$value = \$this->missingNo(\$series, 0, \$n - 1, \$ratio);
// Display missing element
echo("\n ".\$value);
}
}
}

function main()
{
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
\$a = array(1, 5, 25, 625, 3125);
\$b = array(6, 12, 24, 48, 96, 192, 768, 1536);
\$c = array(5, 20, 40, 80, 160);
// Formula : x, xr, xr^2, xr^3...
// Test A
\$n = count(\$a);
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
// Test B
\$n = count(\$b);
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
// Test C
\$n = count(\$c);
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
}
main();``````

#### Output

`````` 125
384
10``````
``````/*
Node JS program
Find the missing number in geometric progression
*/
class GeometricProgression
{
missingNo(series, low, high, ratio)
{
if (low > high)
{
// When no result
return -1;
}
// Find middle element
var mid = low + parseInt((high - low) / 2);
if (mid + 1 <= high &&
(parseInt(series[mid + 1] / series[mid])) != ratio)
{
return series[mid] * ratio;
}
if (mid > 0 &&
(parseInt(series[mid] / series[mid - 1])) != ratio)
{
return series[mid - 1] * ratio;
}
if (parseInt(Math.pow(ratio, mid)) * series[0] == series[mid])
{
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return this.missingNo(series, mid + 1, high, ratio);
}
// Check sequence of left-sided elements from middle.
return this.missingNo(series, low, mid - 1, ratio);
}
missingGeometricPro(series, n)
{
if (n > 1)
{
// When n is more than 1
// Find ratio between sequence
var ratio = parseInt(Math.pow(
parseInt(series[n - 1] / series[0]), 1.0 / n));
// Find missing element
var value = this.missingNo(series, 0, n - 1, ratio);
// Display missing element
process.stdout.write("\n " + value);
}
}
}

function main()
{
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
var a = [1, 5, 25, 625, 3125];
var b = [6, 12, 24, 48, 96, 192, 768, 1536];
var c = [5, 20, 40, 80, 160];
// Formula : x, xr, xr^2, xr^3...
// Test A
var n = a.length;
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
// Test B
n = b.length;
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
// Test C
n = c.length;
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
}
main();``````

#### Output

`````` 125
384
10``````
``````import math
#    Python 3 program
#    Find the missing number in geometric progression
class GeometricProgression :
def missingNo(self, series, low, high, ratio) :
if (low > high) :
#  When no result
return -1

#  Find middle element
mid = low + int((high - low) / 2)
if (mid + 1 <= high and
(int(series[mid + 1] / series[mid])) != ratio) :
return series[mid] * ratio

if (mid > 0 and(int(series[mid] / series[mid - 1])) != ratio) :
return series[mid - 1] * ratio

if (int(ratio ** mid) * series[0] == series[mid]) :
#  When the middle position element is a valid element
#  of the given geometric progression.
#  Check sequence of right-sided elements from middle.
return self.missingNo(series, mid + 1, high, ratio)

#  Check sequence of left-sided elements from middle.
return self.missingNo(series, low, mid - 1, ratio)

def missingGeometricPro(self, series, n) :
if (n > 1) :
#  When n is more than 1
#  Find ratio between sequence
ratio = int(int(series[n - 1] / series[0]) ** (1.0 / n))
#  Find missing element
value = self.missingNo(series, 0, n - 1, ratio)
#  Display missing element
print("\n ", value, end = "")

def main() :
#  Geometric progression series with one missing element
#  Note : Here first and last elements are not considered as missing
a = [1, 5, 25, 625, 3125]
b = [6, 12, 24, 48, 96, 192, 768, 1536]
c = [5, 20, 40, 80, 160]
#  Formula : x, xr, xr^2, xr^3...
#  Test A
n = len(a)
#  arr[] = [1,5,25,625,3125]
#   x = 1
#   r = 5
#  ---------------------------------------------------------------
#   x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
#  [1,   5,    25,    125,    625,   3125 ]
#                      ↑
#  ---------------------------------------------------------------
#  Result : 125
#  Test B
n = len(b)
#  arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
#  x = 6
#  r = 2
#  ---------------------------------------------------------------
#   x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
#  [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
#                                              ↑
#  ---------------------------------------------------------------
#  Result : 384
#  Test C
n = len(c)
#  arr[] = [5,  20,     40,     80,    160 ]
#   x = 5
#   r = 2
#  ---------------------------------------------------------------
#   x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
#   [5,   10,   20,     40,     80,    160 ]
#          ↑
#  ---------------------------------------------------------------
#  Result : 10

if __name__ == "__main__": main()``````

#### Output

``````  125
384
10``````
``````#    Ruby program
#    Find the missing number in geometric progression
class GeometricProgression
def missingNo(series, low, high, ratio)
if (low > high)
#  When no result
return -1
end

#  Find middle element
mid = low + (high - low) / 2
if (mid + 1 <= high &&
(series[mid + 1] / series[mid]) != ratio)
return series[mid] * ratio
end

if (mid > 0 && (series[mid] / series[mid - 1]) != ratio)
return series[mid - 1] * ratio
end

if ((ratio ** mid) * series[0] == series[mid])
#  When the middle position element is a valid element
#  of the given geometric progression.
#  Check sequence of right-sided elements from middle.
return self.missingNo(series, mid + 1, high, ratio)
end

#  Check sequence of left-sided elements from middle.
return self.missingNo(series, low, mid - 1, ratio)
end

def missingGeometricPro(series, n)
if (n > 1)
#  When n is more than 1
#  Find ratio between sequence
ratio = ((series[n - 1] / series[0]) ** ((1.0 / n))).to_i

#  Find missing element
value = self.missingNo(series, 0, n - 1, ratio)
#  Display missing element
print("\n ", value)
end

end

end

def main()
#  Geometric progression series with one missing element
#  Note : Here first and last elements are not considered as missing
a = [1, 5, 25, 625, 3125]
b = [6, 12, 24, 48, 96, 192, 768, 1536]
c = [5, 20, 40, 80, 160]
#  Formula : x, xr, xr^2, xr^3...
#  Test A
n = a.length
#  arr[] = [1,5,25,625,3125]
#   x = 1
#   r = 5
#  ---------------------------------------------------------------
#   x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
#  [1,   5,    25,    125,    625,   3125 ]
#                      ↑
#  ---------------------------------------------------------------
#  Result : 125
#  Test B
n = b.length
#  arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
#  x = 6
#  r = 2
#  ---------------------------------------------------------------
#   x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
#  [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
#                                              ↑
#  ---------------------------------------------------------------
#  Result : 384
#  Test C
n = c.length
#  arr[] = [5,  20,     40,     80,    160 ]
#   x = 5
#   r = 2
#  ---------------------------------------------------------------
#   x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
#   [5,   10,   20,     40,     80,    160 ]
#          ↑
#  ---------------------------------------------------------------
#  Result : 10
end

main()``````

#### Output

`````` 125
384
10``````
``````/*
Scala program
Find the missing number in geometric progression
*/
class GeometricProgression()
{
def missingNo(series: Array[Int], low: Int, high: Int, ratio: Int): Int = {
if (low > high)
{
// When no result
return -1;
}
// Find middle element
var mid: Int = low + (high - low) / 2;
if (mid + 1 <= high && (series(mid + 1) / series(mid)) != ratio)
{
return series(mid) * ratio;
}
if (mid > 0 && (series(mid) / series(mid - 1)) != ratio)
{
return series(mid - 1) * ratio;
}
if (Math.pow(ratio, mid).toInt * series(0) == series(mid))
{
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return missingNo(series, mid + 1, high, ratio);
}
// Check sequence of left-sided elements from middle.
return missingNo(series, low, mid - 1, ratio);
}
def missingGeometricPro(series: Array[Int], n: Int): Unit = {
if (n > 1)
{
// When n is more than 1
// Find ratio between sequence
var ratio: Int = Math.pow(series(n - 1) / series(0), 1.0 / n).toInt;
// Find missing element
var value: Int = missingNo(series, 0, n - 1, ratio);
// Display missing element
print("\n " + value);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: GeometricProgression = new GeometricProgression();
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
var a: Array[Int] = Array(1, 5, 25, 625, 3125);
var b: Array[Int] = Array(6, 12, 24, 48, 96, 192, 768, 1536);
var c: Array[Int] = Array(5, 20, 40, 80, 160);
// Formula : x, xr, xr^2, xr^3...
// Test A
var n: Int = a.length;
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
// Test B
n = b.length;
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
// Test C
n = c.length;
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
}
}``````

#### Output

`````` 125
384
10``````
``````import Foundation;
/*
Swift 4 program
Find the missing number in geometric progression
*/
class GeometricProgression
{
func missingNo(_ series: [Int], _ low: Int, _ high: Int, _ ratio: Int) -> Int
{
if (low > high)
{
// When no result
return -1;
}
// Find middle element
let mid: Int = low + (high - low) / 2;
if (mid + 1 <= high && (series[mid + 1] / series[mid])  != ratio)
{
return series[mid] * ratio;
}
if (mid > 0 && (series[mid] / series[mid - 1])  != ratio)
{
return series[mid - 1] * ratio;
}
if (Int(pow(Double(ratio), Double(mid))) * series[0] == series[mid])
{
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return self.missingNo(series, mid + 1, high, ratio);
}
// Check sequence of left-sided elements from middle.
return self.missingNo(series, low, mid - 1, ratio);
}
func missingGeometricPro(_ series: [Int], _ n: Int)
{
if (n > 1)
{
// When n is more than 1
// Find ratio between sequence
let ratio: Int = Int(pow(
Double(series[n - 1]) / Double(series[0]), 1.0 / Double(n)));
// Find missing element
let value: Int = self.missingNo(series, 0, n - 1, ratio);
// Display missing element
print("\n ", value, terminator: "");
}
}
}
func main()
{
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
let a: [Int] = [1, 5, 25, 625, 3125];
let b: [Int] = [6, 12, 24, 48, 96, 192, 768, 1536];
let c: [Int] = [5, 20, 40, 80, 160];
// Formula : x, xr, xr^2, xr^3...
// Test A
var n: Int = a.count;
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
// Test B
n = b.count;
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
// Test C
n = c.count;
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
}
main();``````

#### Output

``````  125
384
10``````
``````/*
Kotlin program
Find the missing number in geometric progression
*/
class GeometricProgression
{
fun missingNo(series: Array < Int > ,
low: Int, high: Int, ratio: Int): Int
{
if (low > high)
{
// When no result
return -1;
}
// Find middle element
val mid: Int = low + (high - low) / 2;
if (mid + 1 <= high && (series[mid + 1] / series[mid]) != ratio)
{
return series[mid] * ratio;
}
if (mid > 0 && (series[mid] / series[mid - 1]) != ratio)
{
return series[mid - 1] * ratio;
}
if (Math.pow(ratio.toDouble(), mid.toDouble()).toInt() * series[0] ==
series[mid])
{
// When the middle position element is a valid element
// of the given geometric progression.
// Check sequence of right-sided elements from middle.
return this.missingNo(series, mid + 1, high, ratio);
}
// Check sequence of left-sided elements from middle.
return this.missingNo(series, low, mid - 1, ratio);
}
fun missingGeometricPro(series: Array < Int > , n: Int): Unit
{
if (n > 1)
{
// When n is more than 1
// Find ratio between sequence
val ratio: Int = Math.pow(
series[n - 1].toDouble() / series[0].toDouble(),
1.0 / n.toDouble()).toInt();
// Find missing element
val value: Int = this.missingNo(series, 0, n - 1, ratio);
// Display missing element
print("\n " + value);
}
}
}
fun main(args: Array < String > ): Unit
{
// Geometric progression series with one missing element
// Note : Here first and last elements are not considered as missing
val a: Array < Int > = arrayOf(1, 5, 25, 625, 3125);
val b: Array < Int > = arrayOf(6, 12, 24, 48, 96, 192, 768, 1536);
val c: Array < Int > = arrayOf(5, 20, 40, 80, 160);
// Formula : x, xr, xr^2, xr^3...
// Test A
var n: Int = a.count();
// arr[] = [1,5,25,625,3125]
//
//  x = 1
//  r = 5
// ---------------------------------------------------------------
//  x    xr   1*(5²) 1*(5³)  1*(5⁴)  1*(5⁵)
// [1,   5,    25,    125,    625,   3125 ]
//                     ↑
// ---------------------------------------------------------------
// Result : 125
// Test B
n = b.count();
// arr[] = [6, 12, 24, 48, 96, 192, 768, 1536]
//
// x = 6
// r = 2
// ---------------------------------------------------------------
//  x   (6×2) 6×(2²) 6×(2³)  6×(2⁴)  6×(2⁵)  6×(2⁶)  6×(2⁷)  6×(2⁸)
// [6,   12,   24,    48,     96,    192,     384,    768,   1536 ]
//                                             ↑
// ---------------------------------------------------------------
// Result : 384
// Test C
n = c.count();
// arr[] = [5,  20,     40,     80,    160 ]
//
//  x = 5
//  r = 2
// ---------------------------------------------------------------
//  x   (5×2)  5×(2²) 5×(2³)  5×(2⁴)  5×(2⁵)
//  [5,   10,   20,     40,     80,    160 ]
//         ↑
// ---------------------------------------------------------------
// Result : 10
}``````

#### Output

`````` 125
384
10``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.