Product of Complex Numbers using three Multiplication Operation

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 + 22i
Input: 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*c
Adding 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*d
Let 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++
// 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
// 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
# 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 numbersa = 2b = 3c = 4d = 5Â
# Function callprint_product(a, b, c, d) Â
# This code is contributed by Vishal Maurya. |
C#
// 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 |
Javascript
<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)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



