给定一个整数N ,任务是找到N个除数的几何平均值的整数部分。几何均值是一种特殊的平均值,其中我们将数字相乘,然后取平方根(对于两个数字),立方根(对于三个数字),依此类推。
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
方法:可以观察到,将形成一个N值为1,1,1,2,2,2,2,2,3,3,3,3,3,3,3,…的级数。其第N术语是⌊√n⌋。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the integer
// part of the geometric mean
// of the divisors of n
int geometricMean(int n)
{
return sqrt(n);
}
// Driver code
int main()
{
int n = 16;
cout << geometricMean(n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the integer
// part of the geometric mean
// of the divisors of n
static int geometricMean(int n)
{
return (int) Math.sqrt(n);
}
// Driver code
public static void main(String []args)
{
int n = 16;
System.out.println(geometricMean(n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
from math import sqrt
# Function to return the integer
# part of the geometric mean
# of the divisors of n
def geometricMean(n) :
return int(sqrt(n));
# Driver code
if __name__ == "__main__" :
n = 16;
print(geometricMean(n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the integer
// part of the geometric mean
// of the divisors of n
static int geometricMean(int n)
{
return (int) Math.Sqrt(n);
}
// Driver code
public static void Main(String []args)
{
int n = 16;
Console.WriteLine(geometricMean(n));
}
}
// This code is contributed by PrinciRaj1992
Javascript
输出:
4