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 + 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*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++
#include <bits/stdc++.h>
using namespace std;
void print_product(int a, int b,
int c, int d)
{
int prod1 = a * c;
int prod2 = b * d;
int prod3 = (a + b) * (c + d);
int real = prod1 - prod2;
int imag = prod3 - (prod1 + prod2);
cout << real << " + " << imag << "i";
}
int main()
{
int a, b, c, d;
a = 2;
b = 3;
c = 4;
d = 5;
print_product(a, b, c, d);
return 0;
}
Java
class GFG{
static void print_product(int a, int b,
int c, int d)
{
int prod1 = a * c;
int prod2 = b * d;
int prod3 = (a + b) * (c + d);
int real = prod1 - prod2;
int imag = prod3 - (prod1 + prod2);
System.out.println(real + " + " +
imag + "i");
}
public static void main(String[] args)
{
int a = 2;
int b = 3;
int c = 4;
int d = 5;
print_product(a, b, c, d);
}
}
Python3
def print_product(a, b, c, d):
prod1 = a * c
prod2 = b * d
prod3 = (a + b) * (c + d)
real = prod1 - prod2
imag = prod3 - (prod1 + prod2)
print(real, " + ", imag, "i")
a = 2
b = 3
c = 4
d = 5
print_product(a, b, c, d)
C#
using System;
class GFG{
static void print_product(int a, int b,
int c, int d)
{
int prod1 = a * c;
int prod2 = b * d;
int prod3 = (a + b) * (c + d);
int real = prod1 - prod2;
int imag = prod3 - (prod1 + prod2);
Console.Write(real + " + " + imag + "i");
}
public static void Main()
{
int a, b, c, d;
a = 2;
b = 3;
c = 4;
d = 5;
print_product(a, b, c, d);
}
}
Javascript
<script>
function print_product( a, b, c, d)
{
let prod1 = a * c;
let prod2 = b * d;
let prod3 = (a + b) * (c + d);
let real = prod1 - prod2;
let imag = prod3 - (prod1 + prod2);
document.write(real + " + " + imag + "i");
}
let a, b, c, d;
a = 2;
b = 3;
c = 4;
d = 5;
print_product(a, b, c, d);
</script>
Time Complexity: O(1) Auxiliary Space: O(1)
Last Updated :
29 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...