Divide dos números enteros sin usar multiplicación, división y operador mod | Conjunto2

Dados dos números enteros, digamos a y b. Encuentra el cociente después de dividir a por b sin usar la multiplicación, la división y el operador mod.
Ejemplos: 
 

Input: a = 10, b = 3
Output: 3

Input: a = 43, b = -8
Output: -5

Este problema ya ha sido discutido aquí. En esta publicación, se discute un enfoque diferente.
Acercarse : 
 

  1. Sea a/b = c.
  2. Tome registro en ambos lados
  3. registro(a) – registro(b) = registro(c)
  4. Ahora el registro de RHS se puede escribir como exp en LHS
  5. La fórmula final es: exp(log(a) – log(b)) = c

C++

// C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
 
// Returns the quotient of dividend/divisor.
void Divide(int a, int b)
{
    long long dividend = (long long)a;
    long long divisor = (long long)b;
 
    // Calculate sign of divisor i.e.,
    // sign will be negative only if
    // either one of them is negative
    // otherwise it will be positive
 
    long long sign = (dividend < 0) ^ (divisor < 0) ? -1 : 1;
 
    // Remove signs of dividend and divisor
    dividend = abs(dividend);
    divisor = abs(divisor);
 
    // Zero division Exception.
    if (divisor == 0) {
        cout << "Cannot Divide by 0" << endl;
        return;
    }
 
    if (dividend == 0) {
        cout << a << " / " << b << " is equal to : "
             << 0 << endl;
        return;
    }
 
    if (divisor == 1) {
        cout << a << " / " << b << " is equal to : "
             << sign * dividend << endl;
        return;
    }
 
    // Using Formula derived above.
    cout << a << " / " << b << " is equal to : "
         << sign * exp(log(dividend) - log(divisor))
         << endl;
}
 
// Drivers code
int main()
{
    int a = 10, b = 5;
 
    Divide(a, b);
 
    a = 49, b = -7;
    Divide(a, b);
 
    return 0;
}

Java

// Java program for
// above approach
import java.io.*;
 
class GFG
{
static void Divide(int a, int b)
{
    long dividend = (long)a;
    long divisor = (long)b;
 
    // Calculate sign of divisor i.e.,
    // sign will be negative only if
    // either one of them is negative
    // otherwise it will be positive
 
    long sign = (dividend < 0) ^
                (divisor < 0) ? -1 : 1;
 
    // Remove signs of
    // dividend and divisor
    dividend = Math.abs(dividend);
    divisor = Math.abs(divisor);
 
    // Zero division Exception.
    if (divisor == 0)
    {
        System.out.println("Cannot Divide by 0");
        return;
    }
 
    if (dividend == 0)
    {
        System.out.println(a + " / " + b +
                            " is equal to : " + 0);
        return;
    }
 
    if (divisor == 1)
    {
        System.out.println(a + " / " + b +
                           " is equal to : " +
                             sign * dividend);
        return;
    }
 
    // Using Formula
    // derived above.
    System.out.println(a + " / " + b + " is equal to : " +
                                         Math.floor(sign *
                            (Math.exp(Math.log(dividend) -
                             Math.log(divisor)))));
}
 
// Driver code
public static void main (String[] args)
{
int a = 10, b = 5;
 
Divide(a, b);
 
a = 49; b = -7;
Divide(a, b);
}
}
 
// This code is contributed
// by shiv_bhakt.

Python3

# Python3 program
# for above approach
import math
def Divide(a, b):
    dividend = a;
    divisor = b;
 
    # Calculate sign of divisor
    # i.e., sign will be negative
    # only if either one of them
    # is negative otherwise it
    # will be positive
 
    sign = -1 if ((dividend < 0) ^
                  (divisor < 0)) else 1;
 
    # Remove signs of
    # dividend and divisor
    dividend = abs(dividend);
    divisor = abs(divisor);
 
    # Zero division Exception.
    if (divisor == 0):
        print("Cannot Divide by 0");
 
    if (dividend == 0):
        print(a, "/", b, "is equal to :", 0);
 
    if (divisor == 1):
        print(a, "/", b, "is equal to :",
                      (sign * dividend));
 
    # Using Formula
    # derived above.
    print(a, "/", b, "is equal to :",
          math.floor(sign * math.exp(math.log(dividend) -
                                     math.log(divisor))));
 
# Driver code
a = 10;
b = 5;
 
Divide(a, b);
 
a = 49;
b = -7;
Divide(a, b);
 
# This code is contributed
# by mits

C#

// C# program for
// above approach
using System;
 
class GFG
{
static void Divide(int a, int b)
{
    long dividend = (long)a;
    long divisor = (long)b;
 
    // Calculate sign of divisor
    // i.e., sign will be negative
    // only if either one of them
    // is negative otherwise it
    // will be positive
 
    long sign = (dividend < 0) ^
                (divisor < 0) ? -1 : 1;
 
    // Remove signs of
    // dividend and divisor
    dividend = Math.Abs(dividend);
    divisor = Math.Abs(divisor);
 
    // Zero division Exception.
    if (divisor == 0)
    {
        Console.WriteLine("Cannot Divide by 0");
        return;
    }
 
    if (dividend == 0)
    {
        Console.WriteLine(a + " / " + b +
                          " is equal to : " + 0);
        return;
    }
 
    if (divisor == 1)
    {
        Console.WriteLine(a + " / " + b +
                          " is equal to : " +
                            sign * dividend);
        return;
    }
 
    // Using Formula
    // derived above.
    Console.WriteLine(a + " / " + b + " is equal to : " +
                                        Math.Floor(sign *
                           (Math.Exp(Math.Log(dividend) -
                                   Math.Log(divisor)))));
}
 
// Driver code
public static void Main ()
{
    int a = 10, b = 5;
     
    Divide(a, b);
     
    a = 49; b = -7;
    Divide(a, b);
}
}
 
// This code is contributed
// by shiv_bhakt.

PHP

<?php
// PHP program for above approach
 
function Divide($a, $b)
{
    $dividend = $a;
    $divisor = $b;
 
    // Calculate sign of divisor
    // i.e., sign will be negative
    // only if either one of them
    // is negative otherwise it
    // will be positive
 
    $sign = ($dividend < 0) ^
            ($divisor < 0) ? -1 : 1;
 
    // Remove signs of
    // dividend and divisor
    $dividend = abs($dividend);
    $divisor = abs($divisor);
 
    // Zero division Exception.
    if ($divisor == 0)
    {
        echo "Cannot Divide by 0";
        echo"";
    }
 
    if ($dividend == 0)
    {
        echo $a , " / " , $b ,
             " is equal to : " , 0 ;
            echo "";
    }
 
    if ($divisor == 1)
    {
        echo $a , " / " , $b ,
             " is equal to : ",
             $sign * $dividend. "\n";
        echo "";
    }
 
    // Using Formula
    // derived above.
    echo $a , " / " , $b ,
         " is equal to : " ,
         $sign * exp(log($dividend) -
                      log($divisor)). "\n";
        echo "";
}
 
// Driver code
$a = 10;
$b = 5;
 
Divide($a, $b);
 
$a = 49;
$b = -7;
Divide($a, $b);
 
// This code is contributed
// by shiv_bhakt.
?>

Javascript

<script>
 
// Javascript program for
// above approach
 
function Divide(a, b)
{
    var dividend = a;
    var divisor = b;
 
    // Calculate sign of divisor i.e.,
    // sign will be negative only if
    // either one of them is negative
    // otherwise it will be positive
    var sign = (dividend < 0) ^
                (divisor < 0) ? -1 : 1;
 
    // Remove signs of
    // dividend and divisor
    dividend = Math.abs(dividend);
    divisor = Math.abs(divisor);
 
    // Zero division Exception.
    if (divisor == 0)
    {
        document.write("Cannot Divide by 0");
        return;
    }
 
    if (dividend == 0)
    {
        document.write(a + " / " + b +
               " is equal to : " + 0 + "<br>");
        return;
    }
 
    if (divisor == 1)
    {
        System.out.println(a + " / " + b +
                           " is equal to : " +
                             sign * dividend + "<br>");
        return;
    }
 
    // Using Formula
    // derived above.
    document.write(a + " / " + b + " is equal to : " +
                           Math.floor(sign *
                          (Math.exp(Math.log(dividend) -
                          Math.log(divisor)))) + "<br>");
}
 
// Driver Code
var a = 10, b = 5;
 
Divide(a, b);
 
a = 49; b = -7;
Divide(a, b);
 
// This code is contributed by Kirti
 
</script>
Producción: 

10 / 5 is equal to : 2
49 / -7 is equal to : -7

 

Publicación traducida automáticamente

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