# Exponent of complex numbers

Given two integers, which may be negative, zero, or positive, a and b (taken in any reasonable format, including inputting a plain complex number), convert it to a + bi where i is the imaginary number (square root of negative one). Then, raise it to the power of a third (positive integer) input variable, c as to (a + bi)c. You should then end up with something like d + ei. You must then output, or return, d and e in any reasonable format (including outputting a plain complex number).

Input and output may be taken or outputted in any order.

Examples:

5, 2, 2 -> 21, 20
1, 4, 2 -> -15, 8
-5, 0, 1 -> -5, 0

• If we use de Moivre's formula, is floating point imprecison allowed? – Giuseppe Oct 14 '17 at 12:03
• @Giuseppe Yes, that's okay. – Okx Oct 14 '17 at 13:12
• FWIW I think the change to the rules (allowing a fully flexible I/O) made a fairly interesting challenge pretty dull. – Jonathan Allan Oct 15 '17 at 20:50
• @JonathanAllan at least for languages with native complex number support -- which are quite many :( – Felix Palmen Oct 17 '17 at 11:22
• @JonathanAllan I can't please everyone :( – Okx Oct 17 '17 at 12:08

# Mathematica, 17 bytes

ReIm[(#+#2I)^#3]&


Try it online!

-8 bytes from alephalpha

but........ rules have changed...... so

# Mathematica, 5 bytes

Power

• {Re@#,Im@#}& -> ReIm – alephalpha Oct 14 '17 at 8:24
• 17 bytes. You can simply remove @#&. – Mr. Xcoder Oct 14 '17 at 8:31
• haha, yes, my mistake – ZaMoC Oct 14 '17 at 8:33
• You can now do #^#2& or just Power. – totallyhuman Oct 15 '17 at 12:25

# Python 3, 3 bytes

pow


Try it online!

Input and output as complex numbers.

# Python 3, 47 bytes

def f(a,b,c):n=(a+b*1j)**c;return n.real,n.imag


Try it online!

Input and output as integers

# Python 2, 62 60 bytes

-2 bytes thanks to @Leonhard

a,b,c=input();d=1;e=0
exec'd,e=a*d-b*e,a*e+b*d;'*c
print d,e


Try it online!

does not use complex number type

# Javascript (ES6), 51 50 bytes

a=>b=>g=c=>c?([x,y]=g(c-1),[x*a-b*y,a*y+b*x]):"10"

• Takes input in currying form: f(a)(b)(c)
• Returns the result as an array: [d, e]

Explanation

a=>b=>g=c=>               // Input in currying syntax
c?(                   // If c != 0:
[x,y]=g(c-1),     //     Set [x, y] to the result of f(a)(b)(c-1)
[x*a-b*y,a*y+b*x] //     Return (a + bi) * (x + yi)
):                    // Else: (when c = 0)
"10"              //     Return [1, 0] (to end the recursion)


f=a=>b=>g=c=>c?([x,y]=g(c-1),[x*a-b*y,a*y+b*x]):"10"
<div oninput="o.innerText=f(a.value)(b.value)(c.value)"><input id=a type=number value=0><input id=b type=number value=0><input id=c type=number value=1 min=1><pre id=o>

# Pari/GP, 36 bytes

f(a,b,c)=divrem((a+b*x)^c%(x^2+1),x)


Try it online!

# Pari/GP, 36 bytes

f(a,b,c)=[real(d=(a+b*I)^c),imag(d)]


Try it online!

# Actually, 1 byte

ⁿ


Try it online!

Note that the rules have changed and complex numbers are valid I/O types (unfortunately this turns the post into a "perform this exponentiation" challenge). Original answer below.

# Actually, 3 bytes

Çⁿ╫


Try it online!

Returns the values separated by a newline. Takes the inputs in reverse order and returns the results in reverse order (See the tio link).

Çⁿ╫  - Full program. Reversed inputs.

Ç    - Return a+bi.
ⁿ   - Exponentiation.
╫  - Pushes the real and imaginary parts of a.


# Jelly, 1 byte

*


Try it online!

Thanks to Mr. Xcoder for alerting me of rule updates (-6 as a result).
Thanks to someone for alerting me of rule updates (-2 as a result).

First argument: (a+bj)
Second argument: c
Returns: (d+ej)

• 8 bytes – user202729 Oct 14 '17 at 8:43
• 7 bytes – Jonathan Allan Oct 14 '17 at 12:03
• In fact Jonathan's 3 byter would suffice; ḅı*, as the rules have changed and you are now allowed to output a plain complex number. – Mr. Xcoder Oct 15 '17 at 5:10
• @Mr.Xcoder was sleeping when that happened – Erik the Outgolfer Oct 15 '17 at 7:54
• It seems like a * onebyter is ok now as you can take input as a complex – the default. Oct 15 '17 at 11:29

# R, 3 bytes

This is becoming boring. If input and output is allowed as a complex number, there is a builtin for a power function.

^


For example:

> (5+2i)^2
[1] 21+20i
> (1+4i)^2
[1] -15+8i
> (-5+0i)^1
[1] -5+0i


or

> ^(5+2i,2)
[1] 21+20i
> ^(1+4i,2)
[1] -15+8i
> ^(-5+0i,1)
[1] -5+0i


# 05AB1E, 201917 16 bytes

‚UTSsFXâP(‚RŠ‚+


Try it online! Takes three separate inputs in the order b, a, c and outputs an array [d, e]. Edit: Saved 2 bytes thanks to @Datboi. Saved 1 byte thanks to @Adnan. Explanation:

‚                   Join a and b into a pair
U                  Store in variable X
T                 Push 10 to the stack
S                Split into the pair [d, e] = [1, 0]
s               Swap with c
F              Repeat the rest of the program c times
X             Get [a, b]
â            Cartesian product with [d, e]
P           Multiply each pair together [da, db, ea, eb]
Push each product as a separate stack entry
(         Negate eb
‚        Join with ea into a pair [ea, -eb]
R       Reverse the pair [-eb, ea]
Š      Push under da and db
‚     Join them into a pair [da, db]
+    Add the two pairs [da-eb, db+ea]

• Input and output may be taken or outputted in any order. - That means that you can take the first two inputs in reverse order. – Mr. Xcoder Oct 14 '17 at 10:27
• @Mr.Xcoder Thanks, I hadn't noticed that. – Neil Oct 14 '17 at 10:33
• I'm not sure if it matters or not, but calculating the number can also be done with 'jì+³m. – Adnan Oct 14 '17 at 10:53
• You can replace 1 0‚ with TS for -2 bytes :) – Datboi Oct 14 '17 at 22:32
• And P automatically vectorizes, so you don't need the €. – Adnan Oct 15 '17 at 8:18

# C# (.NET Core), 62 38 bytes

a=>c=>System.Numerics.Complex.Pow(a,c)


Try it online!

• You should include the .Real and .Imaginary in your answer.. According to the rule "You must then output, or return, d and e in any reasonable format (not including outputting a plain complex number)" you're not allowed to just output the Complex number. – Kevin Cruijssen Oct 14 '17 at 14:02

# Pyth, 5125 2 bytes

^E


Takes in c first, followed by a+bj.

7 bytes of boilerplate because apparently output as an imaginary number is disallowed. It's been re-allowed! Hurrah! And with taking in a complex number being a reasonable input, we can cut out an additional 3 bytes!

Previous solutions:

^.jEE


When complex numbers were not reasonable inputs.

m,edsd]^.jEE


When complex numbers were not reasonable outputs.

Test Suite.

# 05AB1E, 1 byte

m


Try it online!

Input: c\n(a+bj)
Output: (d+ej)

• Same tip as Neil, 'jì+³m is a different way to calculate the number. – Adnan Oct 14 '17 at 10:58
• @Adnan at least for me it does matter indeed :p – Erik the Outgolfer Oct 14 '17 at 12:35

# J, 10, 7, 1 bytes

^


Takes c as the right argument and the complex number ajb (how you represent a + bi in J) as the left argument.

Try it online!

## Other Solutions

### 7 bytes

Takes the complex number input as a list.

^~j./@]


### 10 bytes

This outputted the a + bi in the list a b.

+.@^~j./@]


I wanted to try something cute like ^~&.(j./) but the inverse of j./ is obviously not defined. Actually, ^~&.(+.inv) works and you can make that ^&.(+.inv) which is also 10 bytes if you reverse the order in which you take the args.

# TI-BASIC, 2522 8 bytes

Takes the complex number and the exponent as input, and stores output in Ans as a complex number. Drastic drop in bytes due to loosened restrictions on input/output.

Prompt C,E
C^E

• You can save 2 bytes with imag({iAns,Ans in the last line (by i I mean the complex number i). – Misha Lavrov Oct 14 '17 at 20:57
• And I guess then one more byte by just combining the two lines into imag({i,1}(A+Bi)^C. – Misha Lavrov Oct 14 '17 at 21:01
• Rules have changed, now you can take input and return complex numbers, if that is of any help. – Erik the Outgolfer Oct 15 '17 at 12:11

# 6502 machine code subroutine, 199187 185 bytes

A2 03 B5 FB 95 26 CA 10 F9 88 D0 01 60 A5 26 85 61 A5 27 85 62 A5 FB 85 63 A5
FC 85 64 A9 20 85 6F D0 36 18 A5 6D 65 65 85 28 A5 6E 65 66 85 29 A5 4B 85 26
A5 4C 85 27 50 CF 38 A5 6D E5 65 85 4B A5 6E E5 66 85 4C A5 28 85 61 A5 29 85
62 A5 FB 85 63 A5 FC 85 64 06 6F A9 00 85 65 85 66 A2 10 46 62 66 61 90 0D A5
63 18 65 65 85 65 A5 64 65 66 85 66 06 63 26 64 CA 10 E6 A9 FF 24 6F 70 B9 30
02 F0 9E A5 65 85 6D A5 66 85 6E 24 6F 30 14 A5 28 85 61 A5 29 85 62 A5 FD 85
63 A5 FE 85 64 06 6F D0 B4 A5 26 85 61 A5 27 85 62 A5 FD 85 63 A5 FE 85 64 06
6F B0 A0

• -12 bytes with improved "spaghetti" structure
• -2 bytes changing the register to pass the exponent, so we can make use of zeropage addressing mode in the initial copy loop

This is position-independent code, just put it somewhere in RAM and call it with a jsr instruction.

The routine takes the (complex) base as two 16bit signed integers (2's complement, little-endian) in $fb/$fc (real) and $fd/$fe (imaginary), and the exponent as an unsigned 8bit integer in the Y register.

The result is returned in $26/$27 (real) and $28/$29 (imaginary).

### Explanation

This is still an interesting challenge on the 6502 CPU as there are no instructions for even multiplying. The approach is straight forward, implementing a complex multiplication and executing it as often as required by the exponent. Golfing is done by avoiding subroutines, instead creating kind of a "branch spaghetti", so the code for doing a simple 16bit multiplication that's needed multiple times is reused with the lowest possible overhead. Here's the commented disassembly:

 .cexp:
A2 03       LDX #$03 ; copy argument ... .copyloop: B5 FB LDA$FB,X
95 26       STA $26,X CA DEX 10 F9 BPL .copyloop ; ... to result .exploop: 88 DEY ; decrement exponent D0 01 BNE .mult ; zero reached -> done 60 RTS .mult: ; multiply (complex) result by argument A5 26 LDA$26             ; prepare to multiply real components
85 61       STA $61 ; (a*c) A5 27 LDA$27
85 62       STA $62 A5 FB LDA$FB
85 63       STA $63 A5 FC LDA$FC
85 64       STA $64 A9 20 LDA #$20            ; marker for where to continue
85 6F       STA $6F D0 36 BNE .mult16 ; branch to 16bit multiplication .mult5: 18 CLC ; calculate sum (a*d) + (b*c) A5 6D LDA$6D
65 65       ADC $65 85 28 STA$28             ; and store to imaginary component of result
A5 6E       LDA $6E 65 66 ADC$66
85 29       STA $29 A5 4B LDA$4B             ; load temporary result (a*c) - (b*d)
85 26       STA $26 ; and store to real component of result A5 4C LDA$4C
85 27       STA $27 50 CF BVC .exploop ; next exponentiation step .mult3: 38 SEC ; calculate difference (a*c) - (b*d) A5 6D LDA$6D
E5 65       SBC $65 85 4B STA$4B             ; and store to temporary location
A5 6E       LDA $6E E5 66 SBC$66
85 4C       STA $4C A5 28 LDA$28             ; prepare to multiply real component of result
85 61       STA $61 ; with imaginary component of argument A5 29 LDA$29             ; (a*d)
85 62       STA $62 A5 FB LDA$FB
85 63       STA $63 A5 FC LDA$FC
85 64       STA $64 06 6F ASL$6F             ; advance "continue marker"
.mult16:
A9 00       LDA #$00 ; initialize 16bit multiplication 85 65 STA$65             ; result with 0
85 66       STA $66 A2 10 LDX #$10            ; bit counter (16)
.m16_loop:
46 62       LSR $62 ; shift arg1 right 66 61 ROR$61
A5 63       LDA $63 ; add arg2 ... 18 CLC 65 65 ADC$65
85 65       STA $65 A5 64 LDA$64
65 66       ADC $66 85 66 STA$66             ; ... to result
06 63       ASL $63 ; shift arg2 left 26 64 ROL$64
CA          DEX                 ; decrement number of bits to go
10 E6       BPL .m16_loop
A9 FF       LDA #$FF ; check marker for where to continue 24 6F BIT$6F
70 B9       BVS .mult3
30 02       BMI .saveres        ; have to save result to temp in 2 cases
F0 9E       BEQ .mult5
.saveres:
A5 65       LDA $65 ; save result to temporary 85 6D STA$6D
A5 66       LDA $66 85 6E STA$6E
24 6F       BIT $6F ; check "continue marker" again 30 14 BMI .mult4 .mult2: A5 28 LDA$28             ; prepare to multiply imaginary components
85 61       STA $61 ; (b*d) A5 29 LDA$29
85 62       STA $62 A5 FD LDA$FD
85 63       STA $63 A5 FE LDA$FE
85 64       STA $64 06 6F ASL$6F             ; advance "continue marker"
D0 B4       BNE .mult16         ; branch to 16bit multiplication
.mult4:
A5 26       LDA $26 ; prepare to multiply imaginary component of 85 61 STA$61             ; result with real component of argument
A5 27       LDA $27 ; (b*c) 85 62 STA$62
A5 FD       LDA $FD 85 63 STA$63
A5 FE       LDA $FE 85 64 STA$64
06 6F       ASL $6F ; advance "continue marker" B0 A0 BCS .mult16 ; branch to 16bit multiplication  ### Example program using it (C64, assembly source in ca65-syntax): .import cexp CEXP_A =$fb
CEXP_AL         = $fb CEXP_AH =$fc
CEXP_B          = $fd CEXP_BL =$fd
CEXP_BH         = $fe CEXP_RA =$26
CEXP_RAL        = $26 CEXP_RAH =$27
CEXP_RB         = $28 CEXP_RBL =$28
CEXP_RBH        = $29 .segment "LDADDR" .word$c000

.segment "MAIN"
jsr     $aefd ; consume comma jsr$ad8a           ; evaluate number
jsr     $b1aa ; convert to 16bit int sty CEXP_AL ; store as first argument sta CEXP_AH jsr$aefd           ; ...
jsr     $ad8a jsr$b1aa
sty     CEXP_BL         ; store as second argument
sta     CEXP_BH
jsr     $b79b ; read 8bit unsigned into X txa ; and transfer tay ; to Y jsr cexp ; call our function lda CEXP_RAH ; read result (real part) ldy CEXP_RAL jsr numout ; output ldx CEXP_RBH ; read result (imaginary part) bmi noplus lda #'+' ; output a + if it's not negative jsr$ffd2
noplus:         txa
ldy     CEXP_RBL
jsr     numout          ; output (imaginary part)
lda     #'i'
jsr     $ffd2 ; output i lda #$0d            ; and newline
jmp     $ffd2 numout: jsr$b391           ; convert to floating point
jsr     $bddd ; format floating point as string ldy #$01
numout_loop:    lda     $ff,y ; output loop bne numout_print ; until 0 terminator found rts numout_print: cmp #' ' ; skip space characters in output beq numout_next jsr$ffd2
numout_next:    iny
bne     numout_loop


## Online demo

Usage: sys49152,[a],[b],[c], e.g. sys49152,5,2,2 (Output: 21+20i)

# Dyalog APL, 10 bytes

⎕*⍨⊣+¯11○⊢


Try it online!

a is left argument, b is right argument, and c via input prompt.

Returns a complex number in the format dJe.

• Rules have changed, now you can take input and return complex numbers, if that's of any help. – Erik the Outgolfer Oct 15 '17 at 12:13

# MATL, 1 byte

^


Inputs are a+jb, c.

Try it online!

## Old version: non-complex input and output, 8 bytes

J*+i^&Zj


Input order is b,a, c.

Try it online!

### Explanation

J           Push imaginary unit
*          Multiply by implicit input b
i        Take input c
^       Power
&Zj    Push real and imaginary parts. Implicitly display

• Multiply by implicit input b - Add implicit input b. Did you mean a in either of those? – Mr. Xcoder Oct 14 '17 at 17:13
• @Mr.Xcoder Yes, thanks. Corrected – Luis Mendo Oct 15 '17 at 2:42
• You can take input in the form of a complex number now, and output in the form of a complex number. You can probably cut out a lot of boilerplate from this answer because of that. – Steven H. Oct 15 '17 at 11:48
• @StevenHewitt Thanks! Edited now – Luis Mendo Oct 15 '17 at 12:36

# C (gcc), 34 bytes

#define f(a,b,c)cpow((a)+1i*(b),c)


Try it online!

# Casio-Basic, 6 bytes

a^b


Change to the rules to allow input and output as complex numbers made this significantly shorter.

3 bytes for the function, +3 to enter a,b in the parameters box.

# 8th, 38 bytes

Code

c:new dup >r ( r@ c:* ) rot n:1- times


SED (Stack Effect Diagram) is: c a b -- (a + bi) ^ c

Warning: a + bi is left on r-stack, but this doesn't affect subsequent computations.

needs math/complex

: f \ c a b  -- (a + bi) ^ c
c:new                      \ create a complex number from a and b
dup                        \ duplicate a + bi
>r                         \ store a + bi on r-stack
( r@ c:* ) rot n:1- times  \ raise ( a + bi ) to c
;


Example and usage

: app:main
\ rdrop is not strictly required
2 5 2 f . cr rdrop
2 1 4 f . cr rdrop
1 -5 0 f . cr rdrop
bye
;


Output of the previous code

c:1:data:{"real":21,"imag":20}
c:1:data:{"real":-15,"imag":8}
c:2:data:{"real":-5,"imag":0}


# Octave / MATLAB, 6 bytes

@power


Anonymous function that inputs two numbers and outputs their power.

Try it online!

## Old version: non-complex input and output, 30 bytes

@(a,b,c)real((a+j*b)^c./[1 j])


Anonymous function that inputs three numbers and outputs an array of two numbers.

Try it online!

# Perl 6,  29 26 20 19  11 bytes

{$_=($^a+$^b*i)**$^c;.re,.im}


Try it

{(($^a+$^b*i)**\$^c).reals}


Try it

((*+* *i)** *).reals


Try it

((*+* *i)***).reals


Try it

With the change of output restrictions it can be further reduced:

(*+* *i)***


Try it

The *** part is parsed as ** * because the ** infix operator is longer than the * infix operator.

## Expanded:

#     __________________ 1st parameter
#    /   _______________ 2nd parameter
#   /   /         ______ 3rd parameter
#  /   /         /
# V   V         V
( * + * * i) ** *
#       ^    ^^
#        \     \________ exponentiation
#         \_____________ multiplication

• You can now do (*+* *i)***. – totallyhuman Oct 15 '17 at 12:19

# R, 25 bytes

simplest - since outputting complex is allowed.

function(a,b,c)(a+b*1i)^c
`