Multiplicar un entero dado por 3,5

Dado un entero x, escribe una función que multiplique x por 3,5 y devuelva el resultado entero. No está permitido usar %, /, *. 

Examples :
Input: 2
Output: 7

Input: 5
Output: 17 (Ignore the digits after decimal point)

Solución: 
1. Podemos obtener x*3.5 sumando 2*x, x y x/2. Para calcular 2*x, desplace x a la izquierda en 1 y para calcular x/2, desplace x a la derecha en 1. 
 A continuación se muestra la implementación del enfoque anterior:

C++

// C++ program to multiply
// a number with 3.5
#include <iostream>
using namespace std;
 
int multiplyWith3Point5(int x)
{
    return (x<<1) + x + (x>>1);
}
 
/* Driver program to test above functions*/
int main()
{
    int x = 4;
    cout << " "<< multiplyWith3Point5(x);
    getchar();
    return 0;
}
 
 
// This code is contributed by shivanisinghss2110.

C

// C++ program to multiply
// a number with 3.5
#include <stdio.h>
 
int multiplyWith3Point5(int x)
{
    return (x<<1) + x + (x>>1);
}
 
/* Driver program to test above functions*/
int main()
{
    int x = 4;
    printf("%d", multiplyWith3Point5(x));
    getchar();
    return 0;
}

Java

// Java Program to multiply
// a number with 3.5
 
class GFG {
         
    static int multiplyWith3Point5(int x)
    {
        return (x<<1) + x + (x>>1);
    }
     
    /* Driver program to test above functions*/
    public static void main(String[] args)
    {
        int x = 2;
        System.out.println(multiplyWith3Point5(x));
    }
}
 
// This code is contributed by prerna saini.

Python3

# Python 3 program to multiply
# a number with 3.5
 
def multiplyWith3Point5(x):
 
    return (x<<1) + x + (x>>1)
  
 
# Driver program to
# test above functions
x = 4
print(multiplyWith3Point5(x))
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# Program to multiply
// a number with 3.5
using System;
 
class GFG
{
         
    static int multiplyWith3Point5(int x)
    {
        return (x<<1) + x + (x>>1);
    }
     
    /* Driver program to test above functions*/
    public static void Main()
    {
        int x = 2;
        Console.Write(multiplyWith3Point5(x));
    }
     
}
 
// This code is contributed by Sam007

PHP

<?php
// PHP program to multiply
// a number with 3.5
 
 
function multiplyWith3Point5( $x)
{
    return ($x << 1) + $x + ($x >> 1);
}
 
// Driver Code
$x = 4;
echo multiplyWith3Point5($x);
     
// This code is contributed by vt_m.
?>

Javascript

<script>
// javascript Program to multiply
// a number with 3.5 
 
function multiplyWith3Point5(x)
{
    return (x<<1) + x + (x>>1);
}
 
/* Driver program to test above functions*/
var x = 4;
document.write(multiplyWith3Point5(x));
 
// This code is contributed by Amit Katiyar
</script>
Producción

14

Complejidad de tiempo : O(1)

Complejidad espacial : O(1)

2. Otra forma de hacer esto podría ser (8*x – x)/2 (ver el código a continuación). Gracias a Ajaym por sugerir esto.

C++

// C++ program approach
 
#include <iostream>
using namespace std;
 
int multiplyWith3Point5(int x)
{
  return ((x<<3) - x)>>1;
}   
 
// This code is contributed by shivanisinghss2110

C

#include <stdio.h>
int multiplyWith3Point5(int x)
{
  return ((x<<3) - x)>>1;
}   

Java

// Java program for above approach
import java.io.*;
 
class GFG
{
 
  // Function
  static int multiplyWith3Point5(int x)
  {
    return ((x<<3) - x)>>1;
  }
}
 
// This code is contributed by shivanisinghss2110

Python3

# Python program for above approach
# Function
def multiplyWith3Point5(x):
   
    return ((x<<3) - x)>>1
   
# This code is contributed by shivanisinghss2110

C#

// C# program for above approach
using System;
 
class GFG{
 
// Function to multiple number
// with 3.5
static int multiplyWith3Point5(int x)
 {
    return ((x<<3) - x)>>1;
  }
}
 
// This code is contributed by shivanisinghss2110.

Javascript

// JavaScript program for above approach
// Function
function multiplyWith3Point5(x)
  {
    return ((x<<3) - x)>>1;
  }
 
// This code is contributed by shivanisinghss2110

Complejidad de tiempo : O(1)

Complejidad espacial : O(1)

Otro enfoque:

Otra forma de hacer esto podría ser haciendo una multiplicación binaria por 7 y luego dividir por 2 usando solo <<, ^, & y >>.

Pero aquí tenemos que mencionar que solo se pueden pasar números positivos a este método.

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

C++

// C++ program for above approach
#include <iostream>
using namespace std;
 
// Function to multiple number
// with 3.5
int multiplyWith3Point5(int x)
{
    int r = 0;
 
    // The 3.5 is 7/2, so multiply  by 7 (x * 7) then divide
    // the result by 2  (result/2) x * 7 -> 7 is 0111 so by
    // doing multiply by 7 it means we do 2 shifting for
    // the number but since we doing multiply we need to
    // take care of carry one.
    int x1Shift = x << 1;
    int x2Shifts = x << 2;
 
    r = (x ^ x1Shift) ^ x2Shifts;
    int c = (x & x1Shift) | (x & x2Shifts) | (x1Shift & x2Shifts);
    while (c > 0) {
        c <<= 1;
        int t = r;
        r ^= c;
        c &= t;
    }
 
    // Then divide by 2
    // r / 2
    r = r >> 1;
    return r;
}
 
// Driver Code
int main()
{
    cout << (multiplyWith3Point5(5));
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta (kriSania804)

C

// C program for above approach
#include <stdio.h>
 
// Function to multiple number
// with 3.5
int multiplyWith3Point5(int x)
{
    int r = 0;
 
    // The 3.5 is 7/2, so multiply  by 7 (x * 7) then divide
    // the result by 2  (result/2) x * 7 -> 7 is 0111 so by
    // doing multiply by 7 it means we do 2 shifting for
    // the number but since we doing multiply we need to
    // take care of carry one.
    int x1Shift = x << 1;
    int x2Shifts = x << 2;
 
    r = (x ^ x1Shift) ^ x2Shifts;
    int c = (x & x1Shift) | (x & x2Shifts) | (x1Shift & x2Shifts);
    while (c > 0) {
        c <<= 1;
        int t = r;
        r ^= c;
        c &= t;
    }
 
    // Then divide by 2
    // r / 2
    r = r >> 1;
    return r;
}
 
// Driver Code
int main()
{
    printf("%d",(multiplyWith3Point5(5)));
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta (kriSania804)

Java

// Java program for above approach
import java.io.*;
 
class GFG
{
    
    // Function to multiple number
    // with 3.5
    static int multiplyWith3Point5(int x)
    {
        int r = 0;
 
        // The 3.5 is 7/2, so multiply
        // by 7 (x * 7) then
        // divide the result by 2
        // (result/2) x * 7 -> 7 is
        // 0111 so by doing multiply
        // by 7 it means we do 2
        // shifting for the number
        // but since we doing
        // multiply we need to take
        // care of carry one.
        int x1Shift = x << 1;
        int x2Shifts = x << 2;
 
        r = (x ^ x1Shift) ^ x2Shifts;
        int c = (x & x1Shift) | (x & x2Shifts)
                | (x1Shift & x2Shifts);
        while (c > 0) {
            c <<= 1;
            int t = r;
            r ^= c;
            c &= t;
        }
 
        // Then divide by 2
        // r / 2
        r = r >> 1;
        return r;
    }
   
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println(multiplyWith3Point5(5));
    }
}

Python3

# Python3 program for the above approach
 
# Function to multiple number
# with 3.5
def multiplyWith3Point5(x):  
    r = 0
 
    # The 3.5 is 7/2, so multiply
    # by 7 (x * 7) then
    # divide the result by 2
    # (result/2) x * 7 -> 7 is
    # 0111 so by doing multiply
    # by 7 it means we do 2
    # shifting for the number
    # but since we doing
    # multiply we need to take
    # care of carry one.   
    x1Shift = x << 1
    x2Shifts = x << 2
 
    r = (x ^ x1Shift) ^ x2Shifts
    c = (x & x1Shift) | (x & x2Shifts) | (x1Shift & x2Shifts)
    while (c > 0):
        c <<= 1
        t = r
        r ^= c
        c &= t
 
    # Then divide by 2
    # r / 2
    r = r >> 1
    return r
 
 # Driver Code
if __name__ == '__main__':
    print(multiplyWith3Point5(5))
 
# This code is contributed by nirajgusain5

C#

// C# program for above approach
using System;
 
class GFG{
 
// Function to multiple number
// with 3.5
static int multiplyWith3Point5(int x)
{
    int r = 0;
 
    // The 3.5 is 7/2, so multiply
    // by 7 (x * 7) then
    // divide the result by 2
    // (result/2) x * 7 -> 7 is
    // 0111 so by doing multiply
    // by 7 it means we do 2
    // shifting for the number
    // but since we doing
    // multiply we need to take
    // care of carry one.
    int x1Shift = x << 1;
    int x2Shifts = x << 2;
 
    r = (x ^ x1Shift) ^ x2Shifts;
    int c = (x & x1Shift) | (x & x2Shifts) |
      (x1Shift & x2Shifts);
       
    while (c > 0)
    {
        c <<= 1;
        int t = r;
        r ^= c;
        c &= t;
    }
 
    // Then divide by 2
    // r / 2
    r = r >> 1;
    return r;
}
 
// Driver Code
public static void Main(string[] args)
{
    Console.WriteLine(multiplyWith3Point5(5));
}
}
 
// This code is contributed by ukasp

Javascript

<script>
 
// Javascript program for the above approach
 
// Function to multiple number
// with 3.5
function multiplyWith3Polet5(x)
{
    let r = 0;
 
    // The 3.5 is 7/2, so multiply
    // by 7 (x * 7) then
    // divide the result by 2
    // (result/2) x * 7 -> 7 is
    // 0111 so by doing multiply
    // by 7 it means we do 2
    // shifting for the number
    // but since we doing
    // multiply we need to take
    // care of carry one.
    let x1Shift = x << 1;
    let x2Shifts = x << 2;
 
    r = (x ^ x1Shift) ^ x2Shifts;
    let c = (x & x1Shift) | (x & x2Shifts) |
      (x1Shift & x2Shifts);
       
    while (c > 0)
    {
        c <<= 1;
        let t = r;
        r ^= c;
        c &= t;
    }
 
    // Then divide by 2
    // r / 2
    r = r >> 1;
    return r;
}
     
// Driver code
document.write(multiplyWith3Polet5(5));   
 
// This code is contributed by avijitmondal1998
 
</script>
Producción

17

Complejidad de tiempo: O (log n)

Complejidad espacial : O(1)

Escriba comentarios si encuentra que el código/algoritmo anterior es incorrecto o encuentra mejores formas de resolver el mismo problema

Publicación traducida automáticamente

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