Program for power of a complex number in O(log n) in C++


Given a complex number in the form of x+yi and an integer n; the task is calculate and print the value of the complex number if we power the complex number by n.

What is a complex number?

A complex number is number which can be written in the form of a + bi, where a and b are the real numbers and i is the solution of the equation or we can say an imaginary number. So, simply putting it we can say that complex number is a combination of Real number and imaginary number.

Raising power of a complex number

To raise the power of a complex number we use the below formula −

(a+bi) (c+di)=( ac−bd )+(ad+bc )i

Like we have a complex number

2+3i and raising its power by 5 then we will get −

(2+3 i)5=(2+3 i)(2+3i)(2+3 i)(2+3 i)(2+3i)

Using the above formula we will get answer −

Example

Input: x[0] = 10, x[1] = 11 /*Where x[0] is the first real number and 11 is the
second real number*/
n = 4
Output: -47959 + i(9240)
Input: x[0] = 2, x[1] =3
n = 5
Output: 122 + i(597)

Approach we are using to solve the above problem −

So, the problem can be solved using iterative method easily but the complexity will be O(n), but we have to solve the problem in O(log n) time. For that we can −

  • First take the input in form of an array.
  • In function Power the x^n
    • Check if n is 1, then return x
    • Recursively call power pass x and n/2 and store its result in a variable sq.
    • Check if dividing n by 2 leaves a remainder 0; if so then return the results obtained from cmul(sq, sq)
    • Check if dividing n by 2 does not leaves a remainder 0; if so then return the results obtained from cmul(x, cmul(sq, sq)).
  • In function cmul().
    • Check if x1 = a+bi and x2 = x+di, then x1 * x2 = (a*c–b*d)+(b*c+d*a)i.
  • Return and printthe results obtained.

Algorithm

Start
Step 1-> declare function to calculate the product of two complex numbers
   long long* complex(long long* part1, long long* part2)
   Declare long long* ans = new long long[2]
   Set ans[0] = (part1[0] * part2[0]) - (part1[1] * part2[1])
   Se ans[1] = (part1[1] * part2[0]) + part1[0] * part2[1]
   return ans
Step 2-> declare function to return the complex number raised to the power n
   long long* power(long long* x, long long n)
   Declare long long* temp = new long long[2]
   IF n = 0
      Set temp[0] = 0
      Set temp[1] = 0
      return temp
   End
   IF n = 1
      return x
   End
   Declare long long* part = power(x, n / 2)
   IF n % 2 = 0
      return complex(part, part)
   End
   return complex(x, complex(part, part))
Step 3 -> In main()
   Declare int n
   Declare and set long long* x = new long long[2]
   Set x[0] = 10
   Set x[1] = -11
   Set n = 4
   Call long long* a = power(x, n)
Stop

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//calculate product of two complex numbers
long long* complex(long long* part1, long long* part2) {
   long long* ans = new long long[2];
   ans[0] = (part1[0] * part2[0]) - (part1[1] * part2[1]);
   ans[1] = (part1[1] * part2[0]) + part1[0] * part2[1];
   return ans;
}
// Function to return the complex number raised to the power n
long long* power(long long* x, long long n) {
   long long* temp = new long long[2];
   if (n == 0) {
      temp[0] = 0;
      temp[1] = 0;
      return temp;
   }
   if (n == 1)
      return x;
      long long* part = power(x, n / 2);
      if (n % 2 == 0)
         return complex(part, part);
         return complex(x, complex(part, part));
}
int main() {
   int n;
   long long* x = new long long[2];
   x[0] = 10;
   x[1] = -11;
   n = 4;
   long long* a = power(x, n);
   cout << a[0] << " + i ( " << a[1] << " )" << endl;
   return 0;
}

Output

power of complex number in O(Log n) : -47959 + i ( 9240 )

Updated on: 23-Dec-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements