Open In App

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

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a complex number of the form x + yi and an integer n, the task is to calculate the value of this complex number raised to the power n.
Examples: 
 

Input: num = 17 - 12i, n = 3
Output: -2431 + i ( -8676 )

Input: num = 18 - 13i, n = 8
Output: 16976403601 + i ( 56580909840 )

 

Approach: This algorithm works in O(log n) time complexity. Let c = a + bi and n is the exponent then, 
 

power(x, n) {
    if(n == 1)
        return x
    sq = power(x, n/2);
    if(n % 2 == 0)
        return cmul(sq, sq);
    if(n % 2 != 0)
        return cmul(x, cmul(sq, sq));
}

As complex number has 2 fields one is real and other is complex hence we store it in an array. We use cmul() function to multiply two arrays where cmul() implements the below multiplication. 
 

If x1 = a + bi and x2 = c + di
then x1 * x2 = a * c - b * d + (b * c + d * a )i 

As we can see in power() function the same function is called for input decreased by 1/2 times. 
T(n) = T(n / 2) + c therefore, T(n) = log n
Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the product
// of two complex numbers
long long* cmul(long long* sq1, long long* sq2)
{
    long long* ans = new long long[2];
 
    // For real part
    ans[0] = (sq1[0] * sq2[0]) - (sq1[1] * sq2[1]);
 
    // For imaginary part
    ans[1] = (sq1[1] * sq2[0]) + sq1[0] * sq2[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* ans = new long long[2];
    if (n == 0) {
        ans[0] = 0;
        ans[1] = 0;
        return ans;
    }
    if (n == 1)
        return x;
 
    // Recursive call for n/2
    long long* sq = power(x, n / 2);
    if (n % 2 == 0)
        return cmul(sq, sq);
    return cmul(x, cmul(sq, sq));
}
 
// Driver code
int main()
{
    int n;
    long long* x = new long long[2];
 
    // Real part of the complex number
    x[0] = 18;
 
    // Imaginary part of the complex number
    x[1] = -13;
    n = 8;
 
    // Calculate and print the result
    long long* a = power(x, n);
    cout << a[0] << " + i ( " << a[1] << " )" << endl;
 
    return 0;
}


Java




// Java implementation of the approach
public class Main
{
    // Function to return the product
    // of two complex numbers
    static long[] cmul(long[] sq1, long[] sq2)
    {
        long[] ans = new long[2];
       
        // For real part
        ans[0] = (sq1[0] * sq2[0]) - (sq1[1] * sq2[1]);
       
        // For imaginary part
        ans[1] = (sq1[1] * sq2[0]) + sq1[0] * sq2[1];
       
        return ans;
    }
       
    // Function to return the complex number
    // raised to the power n
    static long[] power(long[] x, long n)
    {
        long[] ans = new long[2];
        if (n == 0) {
            ans[0] = 0;
            ans[1] = 0;
            return ans;
        }
        if (n == 1)
            return x;
       
        // Recursive call for n/2
        long[] sq = power(x, n / 2);
        if (n % 2 == 0)
            return cmul(sq, sq);
        return cmul(x, cmul(sq, sq));
    }
     
    public static void main(String[] args) {
        long n;
        long[] x = new long[2];
       
        // Real part of the complex number
        x[0] = 18;
       
        // Imaginary part of the complex number
        x[1] = -13;
        n = 8;
       
        // Calculate and print the result
        long[] a = power(x, n);
        System.out.print(a[0] + " + i ( " + a[1] + " )");
    }
}
 
// This code is contributed by divyesh072019.


Python 3




# Python3 implementation of the approach
 
# Function to return the product
# of two complex numbers
def cmul(sq1, sq2):
 
    ans = [0] * 2
 
    # For real part
    ans[0] = ((sq1[0] * sq2[0]) -
              (sq1[1] * sq2[1]))
 
    # For imaginary part
    ans[1] = ((sq1[1] * sq2[0]) +
               sq1[0] * sq2[1])
 
    return ans
 
# Function to return the complex
# number raised to the power n
def power(x, n):
 
    ans = [0] * 2
    if (n == 0):
        ans[0] = 0
        ans[1] = 0
        return ans
     
    if (n == 1):
        return x
 
    # Recursive call for n/2
    sq = power(x, n // 2)
    if (n % 2 == 0):
        return cmul(sq, sq)
    return cmul(x, cmul(sq, sq))
 
# Driver code
if __name__ == "__main__":
 
    x = [0] * 2
 
    # Real part of the complex number
    x[0] = 18
 
    # Imaginary part of the
    # complex number
    x[1] = -13
    n = 8
 
    # Calculate and print the result
    a = power(x, n)
    print(a[0], " + i ( ", a[1], " )")
 
# This code is contributed by ita_c


C#




// C# implementation of the approach
using System;
class GFG {
     
    // Function to return the product
    // of two complex numbers
    static long[] cmul(long[] sq1, long[] sq2)
    {
        long[] ans = new long[2];
      
        // For real part
        ans[0] = (sq1[0] * sq2[0]) - (sq1[1] * sq2[1]);
      
        // For imaginary part
        ans[1] = (sq1[1] * sq2[0]) + sq1[0] * sq2[1];
      
        return ans;
    }
      
    // Function to return the complex number
    // raised to the power n
    static long[] power(long[] x, long n)
    {
        long[] ans = new long[2];
        if (n == 0) {
            ans[0] = 0;
            ans[1] = 0;
            return ans;
        }
        if (n == 1)
            return x;
      
        // Recursive call for n/2
        long[] sq = power(x, n / 2);
        if (n % 2 == 0)
            return cmul(sq, sq);
        return cmul(x, cmul(sq, sq));
    }
 
  static void Main() {
    long n;
    long[] x = new long[2];
  
    // Real part of the complex number
    x[0] = 18;
  
    // Imaginary part of the complex number
    x[1] = -13;
    n = 8;
  
    // Calculate and print the result
    long[] a = power(x, n);
    Console.Write(a[0] + " + i ( " + a[1] + " )");
  }
}
 
// This code is contributed by divyeshrabadiya07.


Javascript




<script>
    // Javascript implementation of the approach
     
    // Function to return the product
    // of two complex numbers
    function cmul(sq1, sq2)
    {
        let ans = new Array(2);
 
        // For real part
        ans[0] = (sq1[0] * sq2[0]) - (sq1[1] * sq2[1]);
 
        // For imaginary part
        ans[1] = (sq1[1] * sq2[0]) + sq1[0] * sq2[1];
 
        return ans;
    }
 
    // Function to return the complex number
    // raised to the power n
    function power(x, n)
    {
        let ans = new Array(2);
        if (n == 0) {
            ans[0] = 0;
            ans[1] = 0;
            return ans;
        }
        if (n == 1)
            return x;
 
        // Recursive call for n/2
        let sq = power(x, n / 2);
        if (n % 2 == 0)
            return cmul(sq, sq);
        return cmul(x, cmul(sq, sq));
    }
 
    let n;
    let x = new Array(2);
   
    // Real part of the complex number
    x[0] = 18;
   
    // Imaginary part of the complex number
    x[1] = -13;
    n = 8;
   
    // Calculate and print the result
    let a = power(x, n);
    document.write(a[0] + " + i ( " + a[1] + " )");
     
    // This code is contributed by decode2207.
</script>


PHP




<?php
// PHP implementation of the approach
 
// Function to return the product
// of two complex numbers
function cmul($sq1, $sq2)
{
    $ans = array();
 
    // For real part
    $ans[0] = ($sq1[0] * $sq2[0]) -
              ($sq1[1] * $sq2[1]);
 
    // For imaginary part
    $ans[1] = ($sq1[1] * $sq2[0]) +
               $sq1[0] * $sq2[1];
 
    return $ans;
}
 
// Function to return the complex
// number raised to the power n
function power($x, $n)
{
    $ans = array();
    if ($n == 0)
    {
        $ans[0] = 0;
        $ans[1] = 0;
        return $ans;
    }
    if ($n == 1)
        return $x;
 
    // Recursive call for n/2
    $sq = power($x, $n / 2);
    if ($n % 2 == 0)
        return cmul($sq, $sq);
    return cmul($x, cmul($sq, $sq));
}
 
// Driver code
$x = array();
 
// Real part of the complex number
$x[0] = 18;
 
// Imaginary part of the complex number
$x[1] = -13;
$n = 8;
 
// Calculate and print the result
$a = power($x, $n);
echo $a[0], " + i ( ", $a[1], " )";
 
// This code is contributed by Ryuga
?>


Output

16976403601 + i ( 56580909840 )

Time complexity : O(log n)

Space complexity : O(log n)

Another Method:

In Python, the cmath module is used to perform complex arithmetic, including logarithm and exponential functions.

Below is the implementation:

Python3




import cmath
 
def complex_exponentiation(a, b):
    return cmath.exp(b * cmath.log(a))
 
a = complex(18, -13)
b = 8
result = complex_exponentiation(a, b)  # a^b
print(result)
 
# This code is contributed by Susobhan Akhuli


Output

(16976403600.999973+56580909839.99995j)

Time complexity : O(1)

Space complexity : O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads