Número más grande que divide a x y es coprimo con y

Dados dos números positivos x e y. Encuentre el entero de máximo valor a tal que: 
 

  1. a divide x es decir x % a = 0
  2. aey son coprimos, es decir mcd(a, y) = 1

Ejemplos: 
 

Input : x = 15
        y = 3 
Output : a = 5
Explanation: 5 is the max integer 
which satisfies both the conditions.
             15 % 5 =0
             gcd(5, 3) = 1
Hence, output is 5. 

Input : x = 14
        y = 28
Output : a = 1
Explanation: 14 % 1 =0
             gcd(1, 28) = 1
Hence, output is 1. 

Enfoque: aquí, primero eliminaremos los factores comunes de x e y de x al encontrar el máximo común divisor (mcd) de xey y dividir x con ese mcd. 
Matemáticamente: 
 

 x = x / gcd(x, y) —— STEP1 

Ahora, repetimos el PASO 1 hasta obtener mcd(x, y) = 1. 
Por último, devolvemos a = x 
 

C++

// CPP program to find the
// Largest Coprime Divisor
 
#include <bits/stdc++.h>
using namespace std;
 
// Recursive function to return gcd
// of a and b
int gcd(int a, int b)
{
    // Everything divides 0
    if (a == 0 || b == 0)
        return 0;
 
    // base case
    if (a == b)
        return a;
 
    // a is greater
    if (a > b)
        return gcd(a - b, b);
    return gcd(a, b - a);
}
 
// function to find largest
// coprime divisor
int cpFact(int x, int y)
{
    while (gcd(x, y) != 1) {
        x = x / gcd(x, y);
    }
    return x;
}
 
// divisor code
int main()
{
    int x = 15;
    int y = 3;
    cout << cpFact(x, y) << endl;
    x = 14;
    y = 28;
    cout << cpFact(x, y) << endl;
    x = 7;
    y = 3;
    cout << cpFact(x, y);
    return 0;
}

Java

// java program to find the
// Largest Coprime Divisor
import java.io.*;
 
class GFG {
    // Recursive function to return gcd
    // of a and b
    static int gcd(int a, int b)
    {
        // Everything divides 0
        if (a == 0 || b == 0)
            return 0;
 
        // base case
        if (a == b)
            return a;
 
        // a is greater
        if (a > b)
            return gcd(a - b, b);
        return gcd(a, b - a);
    }
 
    // function to find largest
    // coprime divisor
    static int cpFact(int x, int y)
    {
        while (gcd(x, y) != 1) {
            x = x / gcd(x, y);
        }
        return x;
    }
 
    // divisor code
    public static void main(String[] args)
    {
        int x = 15;
        int y = 3;
        System.out.println(cpFact(x, y));
        x = 14;
        y = 28;
        System.out.println(cpFact(x, y));
        x = 7;
        y = 3;
        System.out.println(cpFact(x, y));
    }
}
 
// This article is contributed by vt_m.

Python3

# Python3 code to find the
# Largest Coprime Divisor
 
# Recursive function to return
# gcd of a and b
def gcd (a, b):
     
    # Everything divides 0
    if a == 0 or b == 0:
        return 0
     
    # base case
    if a == b:
        return a
         
    # a is greater
    if a > b:
        return gcd(a - b, b)
     
    return gcd(a, b - a)
 
# function to find largest
# coprime divisor
def cpFact(x, y):
    while gcd(x, y) != 1:
        x = x / gcd(x, y)
    return int(x)
     
# divisor code
x = 15
y = 3
print(cpFact(x, y))
x = 14
y = 28
print(cpFact(x, y))
x = 7
y = 3
print(cpFact(x, y))
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# program to find the
// Largest Coprime Divisor
using System;
 
class GFG {
 
    // Recursive function to return gcd
    // of a and b
    static int gcd(int a, int b)
    {
 
        // Everything divides 0
        if (a == 0 || b == 0)
            return 0;
 
        // base case
        if (a == b)
            return a;
 
        // a is greater
        if (a > b)
            return gcd(a - b, b);
 
        return gcd(a, b - a);
    }
 
    // function to find largest
    // coprime divisor
    static int cpFact(int x, int y)
    {
        while (gcd(x, y) != 1) {
            x = x / gcd(x, y);
        }
 
        return x;
    }
 
    // divisor code
    public static void Main()
    {
 
        int x = 15;
        int y = 3;
        Console.WriteLine(cpFact(x, y));
 
        x = 14;
        y = 28;
        Console.WriteLine(cpFact(x, y));
 
        x = 7;
        y = 3;
        Console.WriteLine(cpFact(x, y));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to find the
// Largest Coprime Divisor
 
// Recursive function to
// return gcd of a and b
function gcd($a, $b)
{
    // Everything divides 0
    if ($a == 0 || $b == 0)
        return 0;
 
    // base case
    if ($a == $b)
        return $a;
 
    // a is greater
    if ($a > $b)
        return gcd($a - $b, $b);
    return gcd($a, $b - $a);
}
 
// function to find largest
// coprime divisor
function cpFact( $x, $y)
{
    while (gcd($x, $y) != 1)
    {
        $x = $x / gcd($x, $y);
    }
    return $x;
}
 
// Driver Code
$x = 15;
$y = 3;
echo cpFact($x, $y), "\n";
$x = 14;
$y = 28;
echo cpFact($x, $y), "\n";
$x = 7;
$y = 3;
echo cpFact($x, $y);
 
// This code is contributed by aj_36
?>

Javascript

<script>
 
// Javascript program to find the
// Largest Coprime Divisor
 
// Recursive function to
// return gcd of a and b
function gcd(a, b)
{
    // Everything divides 0
    if (a == 0 || b == 0)
        return 0;
 
    // base case
    if (a == b)
        return a;
 
    // a is greater
    if (a > b)
        return gcd(a - b, b);
    return gcd(a, b - a);
}
 
// function to find largest
// coprime divisor
function cpFact(x, y)
{
    while (gcd(x, y) != 1)
    {
        x = x / gcd(x, y);
    }
    return x;
}
 
// Driver Code
let x = 15;
let y = 3;
document.write(cpFact(x, y) + "<br>");
x = 14;
y = 28;
document.write(cpFact(x, y), "<br>");
x = 7;
y = 3;
document.write(cpFact(x, y));
 
// This code is contributed by gfgking
 
</script>

Producción : 

5
1
7

Publicación traducida automáticamente

Artículo escrito por Abhishek Sharma 44 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 *