Fracción

Una fracción es una razón de dos valores. Las fracciones tienen la forma a/b donde a se llama numerador , b se llama denominador y b no puede ser igual a 0 (ya que la división por 0 no está definida). El denominador da cuantas partes iguales hay. El numerador representa cuántos de estos se toman. Por ejemplo, un medio, ocho quintos, tres cuartos (1/2, 8/5, 3/4). 
 

Hecho sobre la fracción:  

  1. Las fracciones se pueden reducir si el numerador y el denominador tienen el máximo común divisor (mcd) mayor que 1.
  2. Suma y Resta de Fracciones: Al sumar o restar fracciones, estas deben tener el mismo denominador. Si no tienen el mismo denominador, debemos encontrar uno común para ambos. Para hacer esto, primero necesitamos encontrar el múltiplo común más bajo (mcm) de los dos denominadores o multiplicar cada fracción por los números enteros apropiados para que haya el mismo denominador. 
     
  3. Multiplicación y División de Fracciones: Al multiplicar dos fracciones, simplemente multiplica los dos numeradores y multiplica los dos denominadores. Al dividir dos fracciones, la primera fracción debe multiplicarse por el recíproco de la segunda fracción.
  4. Hay tres tipos de fracciones:
    • Fracciones Propias: El numerador es menor que el denominador. Por ejemplo, 1/3, 3/4, 2/7
    • Fracciones Impropias: El numerador es mayor que (o igual) que el denominador. Por ejemplo, 4/3, 11/4, 7/7.
    • Fracciones Mixtas: Un número entero y una fracción propia juntos. Por ejemplo, 1 1/3, 2 1/4, 16 2/5.

¿Cómo sumar dos fracciones?  
Sume dos fracciones a/b y c/d e imprima la respuesta en la forma más simple.

Ejemplos:  

Input:  1/2 + 3/2
Output: 2/1

Input:  1/3 + 3/9
Output: 2/3

Input:  1/5 + 3/15
Output: 2/5

Algoritmo para sumar dos fracciones  

  • Encuentre un denominador común encontrando el MCM (El mínimo común múltiplo) de los dos denominadores.
  • Cambia las fracciones para que tengan el mismo denominador y suma ambos términos.
  • Reduzca la fracción final obtenida a su forma más simple dividiendo tanto el numerador como el denominador por su máximo común divisor.

C++

// C++ program to add 2 fractions
#include <bits/stdc++.h>
using namespace std;
 
// Function to return gcd of a and b
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to convert the obtained fraction
// into it's simplest form
void lowest(int& den3, int& num3)
{
    // Finding gcd of both terms
    int common_factor = gcd(num3, den3);
 
    // Converting both terms into simpler
    // terms by dividing them by common factor
    den3 = den3 / common_factor;
    num3 = num3 / common_factor;
}
 
// Function to add two fractions
void addFraction(int num1, int den1, int num2,
                 int den2, int& num3, int& den3)
{
    // Finding gcd of den1 and den2
    den3 = gcd(den1, den2);
 
    // Denominator of final fraction obtained
    // finding LCM of den1 and den2
    // LCM * GCD = a * b
    den3 = (den1 * den2) / den3;
 
    // Changing the fractions to have same denominator
    // Numerator of the final fraction obtained
    num3 = (num1) * (den3 / den1) + (num2) * (den3 / den2);
 
    // Calling function to convert final fraction
    // into it's simplest form
    lowest(den3, num3);
}
 
// Driver program
int main()
{
    int num1 = 1, den1 = 500, num2 = 2, den2 = 1500, den3, num3;
 
    addFraction(num1, den1, num2, den2, num3, den3);
 
    printf("%d/%d + %d/%d is equal to %d/%d\n", num1, den1,
           num2, den2, num3, den3);
    return 0;
}

Java

// Java program to add 2 fractions
import java.util.*;
 
class GFG
{
static int den3, num3;
 
// Function to return gcd of a and b
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to convert the obtained fraction
// into it's simplest form
static void lowest()
{
    // Finding gcd of both terms
    int common_factor = gcd(num3, den3);
 
    // Converting both terms into simpler
    // terms by dividing them by common factor
    den3 = den3 / common_factor;
    num3 = num3 / common_factor;
}
 
// Function to add two fractions
static void addFraction(int num1, int den1,
                        int num2, int den2)
{
    // Finding gcd of den1 and den2
    den3 = gcd(den1, den2);
 
    // Denominator of final fraction obtained
    // finding LCM of den1 and den2
    // LCM * GCD = a * b
    den3 = (den1 * den2) / den3;
 
    // Changing the fractions to have
    // same denominator.
    // Numerator of the final fraction obtained
    num3 = (num1) * (den3 / den1) +
           (num2) * (den3 / den2);
 
    // Calling function to convert final fraction
    // into it's simplest form
    lowest();
}
 
// Driver Code
public static void main(String[] args)
{
    int num1 = 1, den1 = 500,
        num2 = 2, den2 = 1500;
 
    addFraction(num1, den1, num2, den2);
 
    System.out.printf("%d/%d + %d/%d is equal to %d/%d\n",
                      num1, den1, num2, den2, num3, den3);
}
}
 
// This code is contributed by Rajput-Ji

Python3

# Python3 program to add 2 fractions
 
# Function to return gcd of a and b
def gcd(a, b):
    if (a == 0):
        return b
    return gcd(b % a, a)
 
# Function to convert the obtained
# fraction into it's simplest form
def lowest(den3, num3):
 
    # Finding gcd of both terms
    common_factor = gcd(num3, den3)
 
    # Converting both terms
    # into simpler terms by
    # dividing them by common factor
    den3 = int(den3 / common_factor)
    num3 = int(num3 / common_factor)
    print(num3, "/", den3)
 
# Function to add two fractions
def addFraction(num1, den1, num2, den2):
 
    # Finding gcd of den1 and den2
    den3 = gcd(den1, den2)
 
    # Denominator of final
    # fraction obtained finding
    # LCM of den1 and den2
    # LCM * GCD = a * b
    den3 = (den1 * den2) / den3
 
    # Changing the fractions to
    # have same denominator Numerator
    # of the final fraction obtained
    num3 = ((num1) * (den3 / den1) +
            (num2) * (den3 / den2))
 
    # Calling function to convert
    # final fraction into it's
    # simplest form
    lowest(den3, num3)
 
# Driver Code
num1 = 1; den1 = 500
num2 = 2; den2 = 1500
 
print(num1, "/", den1, " + ", num2, "/",
    den2, " is equal to ", end = "")
     
addFraction(num1, den1, num2, den2)

C#

// C# program to add 2 fractions
using System;
     
class GFG
{
static int den3, num3;
 
// Function to return gcd of a and b
static int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Function to convert the obtained fraction
// into it's simplest form
static void lowest()
{
    // Finding gcd of both terms
    int common_factor = gcd(num3, den3);
 
    // Converting both terms into simpler
    // terms by dividing them by common factor
    den3 = den3 / common_factor;
    num3 = num3 / common_factor;
}
 
// Function to add two fractions
static void addFraction(int num1, int den1,
                        int num2, int den2)
{
    // Finding gcd of den1 and den2
    den3 = gcd(den1, den2);
 
    // Denominator of final fraction obtained
    // finding LCM of den1 and den2
    // LCM * GCD = a * b
    den3 = (den1 * den2) / den3;
 
    // Changing the fractions to have
    // same denominator.
    // Numerator of the final fraction obtained
    num3 = (num1) * (den3 / den1) +
           (num2) * (den3 / den2);
 
    // Calling function to convert final fraction
    // into it's simplest form
    lowest();
}
 
// Driver Code
public static void Main(String[] args)
{
    int num1 = 1, den1 = 500,
        num2 = 2, den2 = 1500;
 
    addFraction(num1, den1, num2, den2);
 
    Console.Write("{0}/{1} + {2}/{3} is equal to {4}/{5}\n",
                        num1, den1, num2, den2, num3, den3);
}
}
 
// This code is contributed by PrinciRaj1992

PHP

<?php
// PHP program to add
// 2 fractions
 
// Function to return
// gcd of a and b
function gcd($a, $b)
{
    if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}
 
// Function to convert the
// obtained fraction into
// it's simplest form
function lowest(&$den3, &$num3)
{
    // Finding gcd of both terms
    $common_factor = gcd($num3, $den3);
 
    // Converting both terms 
    // into simpler terms by
    // dividing them by common factor
     
    $den3 = (int)$den3 / $common_factor;
    $num3 = (int) $num3 / $common_factor;
}
 
// Function to add
// two fractions
function addFraction($num1, $den1, $num2,
                     $den2, &$num3, &$den3)
{
    // Finding gcd of den1 and den2
    $den3 = gcd($den1, $den2);
 
    // Denominator of final
    // fraction obtained finding
    // LCM of den1 and den2
    // LCM * GCD = a * b
    $den3 = ($den1 * $den2) / $den3;
 
    // Changing the fractions to
    // have same denominator Numerator
    // of the final fraction obtained
    $num3 = ($num1) * ($den3 / $den1) +
            ($num2) * ($den3 / $den2);
 
    // Calling function to convert
    // final fraction into it's
    // simplest form
    lowest($den3, $num3);
}
 
// Driver Code
$num1 = 1; $den1 = 500;
$num2 = 2; $den2 = 1500;
$den3; $num3;
addFraction($num1, $den1, $num2,
            $den2, $num3, $den3);
echo $num1, "/", $den1, " + ",
     $num2, "/", $den2, " is equal to ",
               $num3, "/", $den3, "\n";
             
 
?>

Javascript

<script>
      // JavaScript program to add
      // 2 fractions
      // Function to return
      // gcd of a and b
      function gcd(a, b) {
        if (a === 0) return b;
        return gcd(b % a, a);
      }
 
      // Function to convert the
      // obtained fraction into
      // it's simplest form
      function lowest(den3, num3) {
        // Finding gcd of both terms
        var common_factor = gcd(num3, den3);
 
        // Converting both terms
        // into simpler terms by
        // dividing them by common factor
        den3 = parseInt(den3 / common_factor);
        num3 = parseInt(num3 / common_factor);
        return [den3, num3];
      }
 
      // Function to add
      // two fractions
      function addFraction(num1, den1, num2, den2, num3, den3) {
        // Finding gcd of den1 and den2
        den3 = gcd(den1, den2);
 
        // Denominator of final
        // fraction obtained finding
        // LCM of den1 and den2
        // LCM * GCD = a * b
        den3 = (den1 * den2) / den3;
 
        // Changing the fractions to
        // have same denominator Numerator
        // of the final fraction obtained
        num3 = num1 * (den3 / den1) + num2 * (den3 / den2);
 
        // Calling function to convert
        // final fraction into it's
        // simplest form
        return lowest(den3, num3);
      }
 
      // Driver Code
      var num1 = 1,
        den1 = 500,
        num2 = 2,
        den2 = 1500,
        den3,
        num3;
      var [den3, num3] = addFraction(num1, den1, num2, den2, num3, den3);
      document.write(
        num1 +
          "/" +
          den1 +
          " + " +
          num2 +
          "/" +
          den2 +
          " is equal to " +
          num3 +
          "/" +
          den3 +
          "<br>"
      );
    </script>

Producción : 

1/500 + 2/1500 is equal to 1/300

Complejidad del tiempo: O(log(min(a, b)))

Espacio auxiliar: O(log(min(a, b)))

  
Más problemas relacionados con Fracción: 
 

¡Artículos recientes sobre fracciones! 

Publicación traducida automáticamente

Artículo escrito por ABHISHEK TIWARI 13 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 *