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++ program for the above approach #include <bits/stdc++.h> 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 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 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# 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 |
<script> // Javascript program for the above approach // Function to multiply Complex // Numbers with just three // multiplications function 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 Numbers a = 2; b = 3; c = 4; d = 5; // Function Call print_product(a, b, c, d); </script> |
-7 + 22i
Time Complexity: O(1)
Auxiliary Space: O(1)