Dado un número entero N , la tarea es multiplicar el número por 15 sin utilizar los operadores de multiplicación * y división / .
Ejemplos:
Entrada: N = 10
Salida: 150Entrada: N = 7
Salida: 105
Método 1: podemos multiplicar el número entero N por 15 utilizando operadores bit a bit. Primero, desplace el número a la izquierda en 4 bits, lo que equivale a (16 * N) , luego reste el número original N del número desplazado, es decir , ((16 * N) – N), que es igual a 15 * N.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return (15 * N) without // using '*' or '/' operator long multiplyByFifteen(long n) { // prod = 16 * n long prod = (n << 4); // ((16 * n) - n) = 15 * n prod = prod - n; return prod; } // Driver code int main() { long n = 7; cout << multiplyByFifteen(n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return (15 * N) without // using '*' or '/' operator static long multiplyByFifteen(long n) { // prod = 16 * n long prod = (n << 4); // ((16 * n) - n) = 15 * n prod = prod - n; return prod; } // Driver code public static void main(String[] args) { long n = 7; System.out.print(multiplyByFifteen(n)); } }
Python3
# Python3 implementation of the approach # Function to return (15 * N) without # using '*' or '/' operator def multiplyByFifteen(n): # prod = 16 * n prod = (n << 4) # ((16 * n) - n) = 15 * n prod = prod - n return prod # Driver code n = 7 print(multiplyByFifteen(n))
C#
// C# implementation of the approach using System; class GFG { // Function to return (15 * N) without // using '*' or '/' operator static long multiplyByFifteen(long n) { // prod = 16 * n long prod = (n << 4); // ((16 * n) - n) = 15 * n prod = prod - n; return prod; } // Driver code public static void Main() { long n = 7; Console.Write(multiplyByFifteen(n)); } }
PHP
<?php // PHP implementation of the approach // Function to return (15 * N) without // using '*' or '/' operator function multiplyByFifteen($n) { // prod = 16 * n $prod = ($n << 4); // ((16 * n) - n) = 15 * n $prod = $prod - $n; return $prod; } // Driver code $n = 7; echo multiplyByFifteen($n); // This code is contributed by anuj_67.. ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return (15 * N) without // using '*' or '/' operator function multiplyByFifteen(n) { // prod = 16 * n let prod = (n << 4); // ((16 * n) - n) = 15 * n prod = prod - n; return prod; } // Driver code let n = 7; document.write(multiplyByFifteen(n)); // This code is contributed by Surbhi Tyagi. </script>
105
Método 2: También podemos calcular la multiplicación (15 * N) como la suma de (8 * N) + (4 * N) + (2 * N) + N que se puede obtener realizando operaciones de desplazamiento a la izquierda como (8 * N ) = (N << 3) , (4 * N) = (n << 2) y (2 * N) = (n << 1) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return (15 * N) without // using '*' or '/' operator long multiplyByFifteen(long n) { // prod = 8 * n long prod = (n << 3); // Add (4 * n) prod += (n << 2); // Add (2 * n) prod += (n << 1); // Add n prod += n; // (8 * n) + (4 * n) + (2 * n) + n = (15 * n) return prod; } // Driver code int main() { long n = 7; cout << multiplyByFifteen(n); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return (15 * N) without // using '*' or '/' operator static long multiplyByFifteen(long n) { // prod = 8 * n long prod = (n << 3); // Add (4 * n) prod += (n << 2); // Add (2 * n) prod += (n << 1); // Add n prod += n; // (8 * n) + (4 * n) + (2 * n) + n = (15 * n) return prod; } // Driver code public static void main(String[] args) { long n = 7; System.out.print(multiplyByFifteen(n)); } }
Python3
# Python3 implementation of the approach # Function to perform Multiplication def multiplyByFifteen(n): # prod = 8 * n prod = (n << 3) # Add (4 * n) prod += (n << 2) # Add (2 * n) prod += (n << 1) # Add n prod += n # (8 * n) + (4 * n) + (2 * n) + n = (15 * n) return prod # Driver code n = 7 print(multiplyByFifteen(n))
C#
// C# implementation of the approach using System; class GFG { // Function to return (15 * N) without // using '*' or '/' operator static long multiplyByFifteen(long n) { // prod = 8 * n long prod = (n << 3); // Add (4 * n) prod += (n << 2); // Add (2 * n) prod += (n << 1); // Add n prod += n; // (8 * n) + (4 * n) + (2 * n) + n = (15 * n) return prod; } // Driver code public static void Main() { long n = 7; Console.Write(multiplyByFifteen(n)); } }
Javascript
<script> // Javascript implementation of the approach // Function to return (15 * N) without // using '*' or '/' operator function multiplyByFifteen(n) { // prod = 8 * n var prod = (n << 3); // Add (4 * n) prod += (n << 2); // Add (2 * n) prod += (n << 1); // Add n prod += n; // (8 * n) + (4 * n) + (2 * n) + n = (15 * n) return prod; } // Driver code var n = 7; document.write(multiplyByFifteen(n)); // This code is contributed by shikhasingrajput </script>
105
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)