给定一个具有N个正整数的数组。找到数组所有元素的阶乘的GCD。
例子:
Input : arr[] = {3, 4, 8, 6}
Output : 6
Input : arr[] = {13, 24, 8, 5}
Output : 120
方法:要找到所有元素的阶乘的GCD,首先计算所有元素的阶乘,然后找出它们的GCD。但这似乎是一个漫长的过程。两个数字的GCD是将两个数字相除的最大数字。因此,两个数阶乘的GCD是最小数阶乘数本身的值。
例如:GCD为3! (6)和5! (120)是3! (即6)本身。
因此,要找到给定数组所有元素的阶乘的GCD,找到最小的元素,然后打印其阶乘,这将是我们所需的答案。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Implementation of factorial function
int factorial(int n)
{
return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
// Function to find GCD of factorial of
// elements from array
int gcdOfFactorial(int arr[], int n)
{
// find the minimum element of array
int minm = arr[0];
for (int i = 1; i < n; i++)
minm = minm > arr[i] ? arr[i] : minm;
// return the factorial of minimum element
return factorial(minm);
}
// Driver Code
int main()
{
int arr[] = { 9, 12, 122, 34, 15 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << gcdOfFactorial(arr, n);
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Implementation of factorial function
static int factorial(int n)
{
return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
// Function to find GCD of factorial of
// elements from array
static int gcdOfFactorial(int []arr, int n)
{
// find the minimum element of array
int minm = arr[0];
for (int i = 1; i < n; i++)
minm = minm > arr[i] ? arr[i] : minm;
// return the factorial of minimum element
return factorial(minm);
}
// Driver Code
public static void main (String[] args)
{
int []arr = { 9, 12, 122, 34, 15 };
int n = arr.length;
System.out.println(gcdOfFactorial(arr, n));
}
}
// This code is contributed by mits
Python3
# Implementation of factorial function
def factorial(n):
if n == 1 or n == 0:
return 1
else:
return factorial(n - 1) * n
# Function to find GCD of factorial
# of elements from array
def gcdOfFactorial(arr, n):
# find the minimum element
# of array
minm = arr[0]
for i in range(1, n):
if minm > arr[i]:
minm = arr[i]
else:
arr[i] = minm
# return the factorial of
# minimum element
return factorial(minm)
# Driver Code
arr = [9, 12, 122, 34, 15 ]
n = len(arr)
print(gcdOfFactorial(arr, n))
# This code is contributed
# by mohit kumar
C#
// C# implementation of the above approach
using System;
class GFG
{
// Implementation of factorial function
static int factorial(int n)
{
return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
// Function to find GCD of factorial of
// elements from array
static int gcdOfFactorial(int []arr, int n)
{
// find the minimum element of array
int minm = arr[0];
for (int i = 1; i < n; i++)
minm = minm > arr[i] ? arr[i] : minm;
// return the factorial of minimum element
return factorial(minm);
}
// Driver Code
static void Main()
{
int []arr = { 9, 12, 122, 34, 15 };
int n = arr.Length;
Console.WriteLine(gcdOfFactorial(arr, n));
}
}
// This code is contributed by mits
PHP
$arr[$i] ?
$arr[$i] : $minm;
// return the factorial of minimum element
return factorial($minm);
}
// Driver Code
$arr = array( 9, 12, 122, 34, 15 );
$n = count($arr);
echo gcdOfFactorial($arr, $n);
// This code is contributed by Srathore
?>
Javascript
输出:
362880