Reducir la fracción a su forma mínima

Dados dos enteros x e y y donde x es divisible por y . Se puede representar en forma de fracción x/y . La tarea es reducir la fracción a su forma más baja.
Ejemplos: 
 

Input : x = 16, y = 10
Output : x = 8, y = 5

Input : x = 10, y = 8
Output : x = 5, y = 4

Enfoque: Ambos valores x e y serán divisibles por su máximo común divisor. Entonces, si dividimos x e y del mcd (x, y), entonces x e y se pueden reducir a su forma más simple.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ program to reduce a fraction x/y
// to its lowest form
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to reduce a fraction to its lowest form
void reduceFraction(int x, int y)
{
    int d;
    d = __gcd(x, y);
 
    x = x / d;
    y = y / d;
 
    cout << "x = " << x << ", y = " << y << endl;
}
 
// Driver Code
int main()
{
    int x = 16;
    int y = 10;
 
    reduceFraction(x, y);
 
    return 0;
}

Java

// Java program to reduce a fraction x/y
// to its lowest form
class GFG
{
 
// Function to reduce a fraction to its lowest form
static void reduceFraction(int x, int y)
{
    int d;
    d = __gcd(x, y);
 
    x = x / d;
    y = y / d;
 
    System.out.println("x = " + x + ", y = " + y);
}
 
static int __gcd(int a, int b)
{
    if (b == 0)
        return a;
    return __gcd(b, a % b);
     
}
 
// Driver Code
public static void main(String[] args)
{
    int x = 16;
    int y = 10;
 
    reduceFraction(x, y);
}
}
 
/* This code contributed by PrinciRaj1992 */

Python3

# Python3 program to reduce a fraction x/y
# to its lowest form
from math import gcd
 
# Function to reduce a fraction
# to its lowest form
def reduceFraction(x, y) :
     
    d = gcd(x, y);
 
    x = x // d;
    y = y // d;
 
    print("x =", x, ", y =", y);
 
# Driver Code
if __name__ == "__main__" :
 
    x = 16;
    y = 10;
 
    reduceFraction(x, y);
 
# This code is contributed by Ryuga

C#

// C# program to reduce a fraction x/y
// to its lowest form
using System;
 
class GFG
{
  
// Function to reduce a fraction to its lowest form
static void reduceFraction(int x, int y)
{
    int d;
    d = __gcd(x, y);
  
    x = x / d;
    y = y / d;
  
    Console.WriteLine("x = " + x + ", y = " + y);
}
  
static int __gcd(int a, int b)
{
    if (b == 0)
        return a;
    return __gcd(b, a % b);
      
}
  
// Driver Code
public static void Main(String[] args)
{
    int x = 16;
    int y = 10;
  
    reduceFraction(x, y);
}
}
 
// This code has been contributed by 29AjayKumar

PHP

<?php
// PHP program to reduce a fraction x/y
// to its lowest form
 
// Function to reduce a fraction to its lowest form
function reduceFraction($x, $y)
{
    $d;
    $d = __gcd($x, $y);
 
    $x = $x / $d;
    $y = $y / $d;
 
    echo("x = " . $x . ", y = " . $y);
}
 
function __gcd($a, $b)
{
    if ($b == 0)
        return $a;
    return __gcd($b, $a % $b);
     
}
 
// Driver Code
$x = 16;
$y = 10;
 
reduceFraction($x, $y);
 
// This code is contributed by Rajput-Ji
?>

Javascript

<script>
 
// Javascript program to reduce a fraction x/y
// to its lowest form
 
// Function to reduce a fraction to its lowest form
function reduceFraction(x, y)
{
    let d;
    d = __gcd(x, y);
 
    x = parseInt(x / d);
    y = parseInt(y / d);
 
    document.write("x = " + x + ", y = " + y);
}
 
function __gcd(a, b)
{
    if (b == 0)
        return a;
    return __gcd(b, a % b);
      
}
 
// Driver Code
    let x = 16;
    let y = 10;
 
    reduceFraction(x, y);
 
</script>

Salida :  

x = 8, y = 5

Complejidad del tiempo: O(log(max(x,y)))

Espacio auxiliar: O(log(max(x,y)))
 

Publicación traducida automáticamente

Artículo escrito por SURENDRA_GANGWAR 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 *