Contenido de un polinomio

Dada una array arr[] que denota los coeficientes enteros del polinomio, la tarea es encontrar el contenido del polinomio.
 

El contenido de polinomios con coeficientes enteros se define como el máximo común divisor de sus coeficientes enteros.
Eso es para:

F(x) = a m x m + a m-1 x m-1 + ……..+a 1 x + a 0
Entonces, Contenido del Polinomio = mcd(a m , a m-1 , a m-2 …., un 1 , un 0 )

Ejemplos: 

Entrada: arr[] = {9, 30, 12} 
Salida: 3
Explicación:
El polinomio dado puede ser: 9x 2 + 30x + 12
Por lo tanto, Contenido = mcd(9, 30, 12) = 3

Entrada: arr[] = {2, 4, 6}
Salida: 2

 

Enfoque: La idea es encontrar el Máximo común divisor de todos los elementos de la array que se puede calcular encontrando el GCD repetidamente eligiendo dos elementos a la vez. Eso es:

gcd(a, b, c)
 = gcd(gcd(a, b), c)
 = gcd(a, gcd(b, c))
 = gcd(gcd(a, c), b)

A continuación se muestra la implementación del enfoque anterior:

C++

// C++ implementation to find the
// content of the polynomial
 
#include <bits/stdc++.h>
using namespace std;
 
#define newl "\n"
#define ll long long
#define pb push_back
 
// Function to find the content
// of the polynomial
int findContent(int arr[], int n)
{
    int content = arr[0];
 
    // Loop to iterate over the
    // elements of the array
    for (int i = 1; i < n; i++) {
 
        //__gcd(a, b) is a inbuilt
        // function for Greatest
        // Common Divisor
        content = __gcd(content, arr[i]);
    }
 
    return content;
}
 
// Driver Code
int main()
{
    int n = 3;
    int arr[] = { 9, 6, 12 };
 
    // Function call
    cout << findContent(arr, n);
    return 0;
}

Java

// Java implementation to find the
// content of the polynomial
class GFG{
 
// Function to find the content
// of the polynomial
static int findContent(int arr[], int n)
{
    int content = arr[0];
 
    // Loop to iterate over the
    // elements of the array
    for(int i = 1; i < n; i++)
    {
         
        //__gcd(a, b) is a inbuilt
        // function for Greatest
        // Common Divisor
        content = __gcd(content, arr[i]);
    }
    return content;
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 3;
    int arr[] = { 9, 6, 12 };
 
    // Function call
    System.out.print(findContent(arr, n));
}
}
 
// This code is contributed by sapnasingh4991

Python3

# Python3 implementation to find the
# content of the polynomial
from math import gcd
 
# Function to find the content
# of the polynomial
def findContent(arr, n):
     
    content = arr[0]
 
    # Loop to iterate over the
    # elements of the array
    for i in range(1, n):
 
        # __gcd(a, b) is a inbuilt
        # function for Greatest
        # Common Divisor
        content = gcd(content, arr[i])
 
    return content
 
# Driver Code
if __name__ == '__main__':
     
    n = 3
    arr = [ 9, 6, 12 ]
 
    # Function call
    print(findContent(arr, n))
 
# This code is contributed by mohit kumar 29

C#

// C# implementation to find the
// content of the polynomial
using System;
 
class GFG{
 
// Function to find the content
// of the polynomial
static int findContent(int []arr, int n)
{
    int content = arr[0];
 
    // Loop to iterate over the
    // elements of the array
    for(int i = 1; i < n; i++)
    {
         
        //__gcd(a, b) is a inbuilt
        // function for Greatest
        // Common Divisor
        content = __gcd(content, arr[i]);
    }
    return content;
}
 
static int __gcd(int a, int b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 3;
    int []arr = { 9, 6, 12 };
 
    // Function call
    Console.Write(findContent(arr, n));
}
}
 
// This code is contributed by PrinciRaj1992

Javascript

<script>
 
// Javascript implementation to find the
// content of the polynomial
 
// Function to find the content
// of the polynomial
function findContent(arr, n)
{
    var content = arr[0];
 
    // Loop to iterate over the
    // elements of the array
    for(var i = 1; i < n; i++)
    {
         
        //__gcd(a, b) is a inbuilt
        // function for Greatest
        // Common Divisor
        content = __gcd(content, arr[i]);
    }
    return content;
}
 
function __gcd(a, b)
{
    return b == 0 ? a : __gcd(b, a % b);    
}
 
// Driver Code
var n = 3;
var arr = [ 9, 6, 12 ];
 
// Function call
document.write(findContent(arr, n));
 
// This code is contributed by kirti
 
</script>
Producción: 

3

 

Publicación traducida automáticamente

Artículo escrito por jojo9911 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *