📜  使用三个乘法运算的复数乘积

📅  最后修改于: 2021-04-26 06:33:09             🧑  作者: Mango

给定四个表示形式(a + bi)(c + di)的两个复数的整数a,b,c和d ,任务是仅使用三个乘法运算来找到给定复数的乘积。

例子:

天真的方法:天真的方法是将给定的两个复数直接相乘为:

上述操作将需要四次乘法才能找到两个复数的乘积。

高效方法:上述方法需要四次乘法才能找到乘积。它可以简化为三个乘法:

两个复数的相乘如下:

简化实部:

简化虚构部分,如下所示:

因此,我们需要找到prod1 = a * cprod2 = b * dprod3 =(a + b)*(c + d)的值

因此,我们的最终答案将是:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
  
// Function to multiply Complex
// Numbers with just three
// multiplications
void print_product(int a, int b,
                   int c, int d)
{
    // Find value of prod1, prod2 and prod3
    int prod1 = a * c;
    int prod2 = b * d;
    int prod3 = (a + b) * (c + d);
  
    // Real Part
    int real = prod1 - prod2;
  
    // Imaginary Part
    int imag = prod3 - (prod1 + prod2);
  
    // Print the result
    cout << real << " + " << imag << "i";
}
  
// Driver Code
int main()
{
    int a, b, c, d;
  
    // Given four Numbers
    a = 2;
    b = 3;
    c = 4;
    d = 5;
  
    // Function Call
    print_product(a, b, c, d);
    return 0;
}


Java
// Java program for the above approach
class GFG{
  
// Function to multiply Complex
// Numbers with just three
// multiplications
static void print_product(int a, int b,
                          int c, int d)
{
      
    // Find value of prod1, prod2 and prod3
    int prod1 = a * c;
    int prod2 = b * d;
    int prod3 = (a + b) * (c + d);
  
    // Real Part
    int real = prod1 - prod2;
  
    // Imaginary Part
    int imag = prod3 - (prod1 + prod2);
  
    // Print the result
    System.out.println(real + " + " +
                       imag + "i");
}
  
// Driver Code
public static void main(String[] args)
{
      
    // Given four numbers
    int a = 2;
    int b = 3;
    int c = 4;
    int d = 5;
      
    // Function call
    print_product(a, b, c, d);
}
}
  
// This code is contributed by Pratima Pandey


Python3
# Python3 program for the above approach 
  
# Function to multiply Complex 
# Numbers with just three 
# multiplications 
def print_product(a, b, c, d):
      
    # Find value of prod1, prod2 
    # and prod3
    prod1 = a * c
    prod2 = b * d
    prod3 = (a + b) * (c + d)
  
    # Real part
    real = prod1 - prod2
  
    # Imaginary part
    imag = prod3 - (prod1 + prod2)
  
    # Print the result
    print(real, " + ", imag, "i") 
  
# Driver code
  
# Given four numbers
a = 2
b = 3
c = 4
d = 5
  
# Function call
print_product(a, b, c, d) 
  
# This code is contributed by Vishal Maurya.


C#
// C# program for the above approach
using System;
class GFG{
  
// Function to multiply Complex
// Numbers with just three
// multiplications
static void print_product(int a, int b,
                          int c, int d)
{
    // Find value of prod1, prod2 and prod3
    int prod1 = a * c;
    int prod2 = b * d;
    int prod3 = (a + b) * (c + d);
  
    // Real Part
    int real = prod1 - prod2;
  
    // Imaginary Part
    int imag = prod3 - (prod1 + prod2);
  
    // Print the result
    Console.Write(real + " + " + imag + "i");
}
  
// Driver Code
public static void Main()
{
    int a, b, c, d;
  
    // Given four Numbers
    a = 2;
    b = 3;
    c = 4;
    d = 5;
  
    // Function Call
    print_product(a, b, c, d);
}
}
  
// This code is contributed by Code_Mech


输出:
-7 + 22i

时间复杂度: O(1)
辅助空间: O(1)