Product of Complex Numbers using three Multiplication Operation

29 Nov, 2021
Given four integers a, b, c, and d which represents two complex numbers of the form (a + bi) and (c + di), the task is to find the product of the given complex numbers using only three multiplication operations.Examples: 
Input: a = 2, b = 3, c = 4 and d = 5 Output: -7 + 22i Explanation: Product is given by: (2 + 3i)*(4 + 5i) = 2*4 + 4*3i + 2*5i + 3*5*(-1) = 8 – 15 + (12 + 10)i = -7 + 22iInput: a = 3, b = 7, c = 6 and d = 2 Output: 4 + 48i  
 
Naive Approach: The naive approach is to directly multiply given two complex numbers as: 
=> (a + bi)*(c + di) => a(c + di) + b*i(c + di) => a*c + ad*i + b*c*i + b*d*i*i => (a*c – b*d) + (a*d + b*c)*i  
The above operations would required four multiplication to find the product of two complex number.Efficient Approach: The above approach required four multiplication to find the product. It can be reduced to three multiplication as:Multiplication of two Complex Numbers is as follows: 
(a + bi)*(c + di) = a*c – b*d + (a*d + b*c)i  
Simplify real part:
real part = a*c – b*d Let prod1 = a*c and prod2 = b*d. Thus, real part = prod1 – prod2  
Simplify the imaginary part as follows: 
imaginary part = a*d + b*cAdding and subtracting a*c and b*d in the above imaginar part we have,imaginary part = a*c – a*c + a*d + b*c + b*d – b*d, On rearranging the terms we get, => a*b + b*c + a*d + b*d – a*c – b*d => (a + b)*c + (a + b)*d – a*c – b*d => (a + b)*(c + d) – a*c – b*dLet prod3 = (a + b)*(c + d) Then the imaginary part is given by prod3 – (prod1 + prod2).  
Thus, we need to find the value of prod1 = a * c, prod2 = b * d, and prod3 = ( a + b ) * ( c + d ).So, our final answer will be:  
Real Part = prod1 – prod2 Imaginary Part = prod3 – (prod1 + prod2)  
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to multiply Complex// Numbers with just three// multiplicationsvoid 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 Codeint 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 program for the above approachclass GFG{ // Function to multiply Complex// Numbers with just three// multiplicationsstatic 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 Codepublic 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 program for the above approach # Function to multiply Complex# Numbers with just three# multiplicationsdef 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 numbersa = 2b = 3c = 4d = 5 # Function callprint_product(a, b, c, d) # This code is contributed by Vishal Maurya.
// C# program for the above approachusing System;class GFG{ // Function to multiply Complex// Numbers with just three// multiplicationsstatic 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 Codepublic 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
<script> // Javascript program for the above approach // Function to multiply Complex// Numbers with just three// multiplicationsfunction print_product( a, b, c, d){    // Find value of prod1, prod2 and prod3    let prod1 = a * c;    let prod2 = b * d;    let prod3 = (a + b) * (c + d);     // Real Part    let real = prod1 - prod2;     // Imaginary Part    let imag = prod3 - (prod1 + prod2);     // Print the result    document.write(real + " + " + imag + "i");}  // Driver Code let a, b, c, d; // Given four Numbersa = 2;b = 3;c = 4;d = 5; // Function Callprint_product(a, b, c, d); </script>
-7 + 22i
 
Time Complexity: O(1) Auxiliary Space: O(1)
vishu2908
Code_Mech
dewantipandeydp
jana_sayantan
kk9826225
Numbers
Mathematical
School Programming
Mathematical
Numbers
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Algorithm to solve Rubik's Cube
Merge two sorted arrays with O(1) extra space
Sum of the series (1*2) + (2*3) + (3*4) + ...... upto n terms
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java