Encuentre dos números cuya suma y MCD estén dados

Dada la suma y mcd de dos números  a  b  . La tarea es encontrar los números a y b . Si los números no existen, imprima  -1  .
Ejemplos: 
 

Entrada: suma = 6, mcd = 2 
Salida: a = 4, b = 2 
4 + 2 = 6 y MCD(4, 2) = 2
Entrada: suma = 7, mcd = 2 
Salida: -1 
No existen tales números cuya suma es 7 y MCD es 2 
 

Enfoque: como se da GCD , se sabe que ambos números serán múltiplos de él. 
 

  • Elija el primer número como mcd , luego el otro número será suma – mcd .
  • Si la suma de los dos números elegidos en el paso anterior es igual a la suma , imprima ambos números.
  • De lo contrario, los números no existen e imprimen -1 en su lugar.

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

C++

// C++ program to find two numbers
// whose sum and GCD is given
#include <bits/stdc++.h>
using namespace std;
 
// Function to find two numbers
// whose sum and gcd is given
void findTwoNumbers(int sum, int gcd)
{
    // sum != gcd checks that both the
    // numbers are positive or not
    if (__gcd(gcd, sum - gcd) == gcd && sum != gcd)
        cout << "a = " << min(gcd, sum - gcd)
             << ", b = " << sum - min(gcd, sum - gcd)
             << endl;
    else
        cout << -1 << endl;
}
 
// Driver code
int main()
{
    int sum = 8;
    int gcd = 2;
 
    findTwoNumbers(sum, gcd);
 
    return 0;
}

Java

// Java program to find two numbers
// whose sum and GCD is given
import java.util.*;
class Solution{
 
//function to find gcd of two numbers
static int __gcd(int a,int b)
{
    if (b==0) return a;
   return __gcd(b,a%b);
}
     
// Function to find two numbers
// whose sum and gcd is given
static void findTwoNumbers(int sum, int gcd)
{
    // sum != gcd checks that both the
    // numbers are positive or not
    if (__gcd(gcd, sum - gcd) == gcd && sum != gcd)
        System.out.println(  "a = " + Math.min(gcd, sum - gcd)
            + ", b = " + (int)(sum - Math.min(gcd, sum - gcd)) );
    else
        System.out.println( -1 );
}
 
// Driver code
public static void main(String args[])
{
    int sum = 8;
    int gcd = 2;
 
    findTwoNumbers(sum, gcd);
 
}
 
 
}
//contributed by Arnab Kundu

Python3

# Python 3 program to find two numbers
# whose sum and GCD is given
from math import gcd as __gcd
 
# Function to find two numbers
# whose sum and gcd is given
def findTwoNumbers(sum, gcd):
     
    # sum != gcd checks that both the
    # numbers are positive or not
    if (__gcd(gcd, sum - gcd) == gcd and
                          sum != gcd):
        print("a =", min(gcd, sum - gcd),
              ", b =", sum - min(gcd, sum - gcd))
    else:
        print(-1)
         
# Driver code
if __name__ == '__main__':
    sum = 8
    gcd = 2
 
    findTwoNumbers(sum, gcd)
 
# This code is contributed by
# Surendra_Gangwar

C#

// C# program to find two numbers
// whose sum and GCD is given
using System;
class GFG
{
 
// function to find gcd of two numbers
static int __gcd(int a, int b)
{
    if (b == 0)
        return a;
    return __gcd(b, a % b);
}
     
// Function to find two numbers
// whose sum and gcd is given
static void findTwoNumbers(int sum, int gcd)
{
    // sum != gcd checks that both the
    // numbers are positive or not
    if (__gcd(gcd, sum - gcd) == gcd && sum != gcd)
        Console.WriteLine("a = " + Math.Min(gcd, sum - gcd) +
            ", b = " + (int)(sum - Math.Min(gcd, sum - gcd)));
    else
        Console.WriteLine( -1 );
}
 
// Driver code
public static void Main()
{
    int sum = 8;
    int gcd = 2;
 
    findTwoNumbers(sum, gcd);
}
}
 
// This code is contributed by anuj_67..

PHP

<?php
// PHP program to find two numbers
// whose sum and GCD is given
 
// Function to find gcd of two numbers
function __gcd($a, $b)
{
    if ($b == 0)
        return $a;
         
    return __gcd($b, $a % $b);
}
     
// Function to find two numbers
// whose sum and gcd is given
function findTwoNumbers($sum, $gcd)
{
    // sum != gcd checks that both the
    // numbers are positive or not
    if (__gcd($gcd, $sum - $gcd) == $gcd &&
                            $sum != $gcd)
        echo "a = " , min($gcd, $sum - $gcd),
             " b = ", (int)($sum - min($gcd,
                            $sum - $gcd));
    else
        echo (-1);
}
 
// Driver code
$sum = 8;
$gcd = 2;
 
findTwoNumbers($sum, $gcd);
 
// This code is contributed by Sachin
?>

Javascript

<script>
 
// Javascript program to find two numbers
// whose sum and GCD is given
 
//function to find gcd of two numbers
function __gcd(a, b)
{
    if (b==0) return a;
   return __gcd(b,a%b);
}
     
// Function to find two numbers
// whose sum and gcd is given
function findTwoNumbers(sum, gcd)
{
    // sum != gcd checks that both the
    // numbers are positive or not
    if (__gcd(gcd, sum - gcd) == gcd && sum != gcd)
        document.write(  "a = " + Math.min(gcd, sum - gcd)
            + ", b = " + (sum - Math.min(gcd, sum - gcd)) );
    else
        document.write( -1 );
}
 
// Driver code
 
    let sum = 8;
    let gcd = 2;
 
    findTwoNumbers(sum, gcd);
 
</script>
Producción: 

a = 2, b = 6

 

Complejidad de tiempo: O(log(min(a, b))), donde a y b son dos parámetros de gcd.

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

Publicación traducida automáticamente

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