Given an integer N, the task is to find the integer part of the geometric mean of the divisors of N. The Geometric Mean is a special type of average where we multiply the numbers together and then take a square root (for two numbers), cube root (for three numbers), and so on.
Examples:
Input: N = 4
Output: 2
Divisors of 4 are 1, 2 and 4
Geometric mean = (1 * 2 * 4)(1/3) = 8(1/3) = 2
Input: N = 16
Output: 8
Divisors of 16 are 1, 2, 4, 8 and 16
Geometric mean = (1 * 2 * 4 * 8 * 16)(1/5) = 1024(1/5) = 4
Approach: It can be observed that a series will be formed for the values of N as 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, ….. whose Nth term is ??n?.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int geometricMean( int n)
{
return sqrt (n);
}
int main()
{
int n = 16;
cout << geometricMean(n);
return 0;
}
|
Java
class GFG
{
static int geometricMean( int n)
{
return ( int ) Math.sqrt(n);
}
public static void main(String []args)
{
int n = 16 ;
System.out.println(geometricMean(n));
}
}
|
Python3
from math import sqrt
def geometricMean(n) :
return int (sqrt(n));
if __name__ = = "__main__" :
n = 16 ;
print (geometricMean(n));
|
C#
using System;
class GFG
{
static int geometricMean( int n)
{
return ( int ) Math.Sqrt(n);
}
public static void Main(String []args)
{
int n = 16;
Console.WriteLine(geometricMean(n));
}
}
|
Javascript
<script>
function geometricMean(n)
{
return Math.sqrt(n);
}
var n = 16;
document.write(geometricMean(n));
</script>
|
Time Complexity: O(logn) because using inbuilt sqrt function
Auxiliary Space: O(1)