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
-
\$\begingroup\$ If we use de Moivre's formula, is floating point imprecison allowed? \$\endgroup\$Giuseppe– Giuseppe2017年10月14日 12:03:04 +00:00Commented Oct 14, 2017 at 12:03
-
\$\begingroup\$ @Giuseppe Yes, that's okay. \$\endgroup\$Okx– Okx2017年10月14日 13:12:59 +00:00Commented Oct 14, 2017 at 13:12
-
4\$\begingroup\$ FWIW I think the change to the rules (allowing a fully flexible I/O) made a fairly interesting challenge pretty dull. \$\endgroup\$Jonathan Allan– Jonathan Allan2017年10月15日 20:50:10 +00:00Commented Oct 15, 2017 at 20:50
-
\$\begingroup\$ @JonathanAllan at least for languages with native complex number support -- which are quite many :( \$\endgroup\$Felix Palmen– Felix Palmen2017年10月17日 11:22:54 +00:00Commented Oct 17, 2017 at 11:22
-
\$\begingroup\$ @JonathanAllan I can't please everyone :( \$\endgroup\$Okx– Okx2017年10月17日 12:08:03 +00:00Commented Oct 17, 2017 at 12:08
23 Answers 23
Mathematica, 17 bytes
ReIm[(#+#2I)^#3]&
-8 bytes from alephalpha
but........ rules have changed...... so
Mathematica, 5 bytes
Power
-
5\$\begingroup\$
{Re@#,Im@#}&->ReIm\$\endgroup\$alephalpha– alephalpha2017年10月14日 08:24:03 +00:00Commented Oct 14, 2017 at 8:24 -
1\$\begingroup\$ 17 bytes. You can simply remove
@#&. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年10月14日 08:31:49 +00:00Commented Oct 14, 2017 at 8:31 -
\$\begingroup\$ haha, yes, my mistake \$\endgroup\$ZaMoC– ZaMoC2017年10月14日 08:33:00 +00:00Commented Oct 14, 2017 at 8:33
-
\$\begingroup\$ You can now do
#^#2&or justPower. \$\endgroup\$totallyhuman– totallyhuman2017年10月15日 12:25:21 +00:00Commented Oct 15, 2017 at 12:25
Python 3, 3 bytes
pow
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
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
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)
Pari/GP, 36 bytes
f(a,b,c)=[real(d=(a+b*I)^c),imag(d)]
Jelly, 1 byte
*
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)
-
\$\begingroup\$ 8 bytes \$\endgroup\$user202729– user2027292017年10月14日 08:43:41 +00:00Commented Oct 14, 2017 at 8:43
-
\$\begingroup\$ 7 bytes \$\endgroup\$Jonathan Allan– Jonathan Allan2017年10月14日 12:03:35 +00:00Commented Oct 14, 2017 at 12:03
-
\$\begingroup\$ In fact Jonathan's 3 byter would suffice;
ḅı*, as the rules have changed and you are now allowed to output a plain complex number. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年10月15日 05:10:19 +00:00Commented Oct 15, 2017 at 5:10 -
\$\begingroup\$ @Mr.Xcoder was sleeping when that happened \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年10月15日 07:54:14 +00:00Commented Oct 15, 2017 at 7:54
-
1\$\begingroup\$ It seems like a * onebyter is ok now as you can take input as a complex \$\endgroup\$the default.– the default.2017年10月15日 11:29:27 +00:00Commented Oct 15, 2017 at 11:29
Actually, 1 byte
n
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
Çn╫
Returns the values separated by a newline. Takes the inputs in reverse order and returns the results in reverse order (See the tio link).
Çn╫ - Full program. Reversed inputs. Ç - Return a+bi. n - Exponentiation. ╫ - Pushes the real and imaginary parts of a.
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, (削除) 20 (削除ここまで) (削除) 19 (削除ここまで) (削除) 17 (削除ここまで) 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]
-
\$\begingroup\$ Input and output may be taken or outputted in any order. - That means that you can take the first two inputs in reverse order. \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年10月14日 10:27:20 +00:00Commented Oct 14, 2017 at 10:27
-
\$\begingroup\$ @Mr.Xcoder Thanks, I hadn't noticed that. \$\endgroup\$Neil– Neil2017年10月14日 10:33:35 +00:00Commented Oct 14, 2017 at 10:33
-
-
\$\begingroup\$ You can replace
1 0‚withTSfor -2 bytes :) \$\endgroup\$Datboi– Datboi2017年10月14日 22:32:25 +00:00Commented Oct 14, 2017 at 22:32 -
\$\begingroup\$ And
Pautomatically vectorizes, so you don't need the€. \$\endgroup\$Adnan– Adnan2017年10月15日 08:18:34 +00:00Commented Oct 15, 2017 at 8:18
-
\$\begingroup\$ You should include the
.Realand .Imaginary` in your answer.. According to the rule "You must then output, or return,dandein any reasonable format (not including outputting a plain complex number)" you're not allowed to just output the Complex number. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年10月14日 14:02:57 +00:00Commented Oct 14, 2017 at 14:02
Pyth, (削除) 5 (削除ここまで) (削除) 12 (削除ここまで) (削除) 5 (削除ここまで) 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.
-
-
\$\begingroup\$ @Adnan at least for me it does matter indeed :p \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年10月14日 12:35:34 +00:00Commented Oct 14, 2017 at 12:35
J, (削除) 10 (削除ここまで), (削除) 7 (削除ここまで), 1 byte(削除) s (削除ここまで)
^
Takes c as the right argument and the complex number ajb (how you represent a + bi in J) as the left argument.
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, (削除) 25 (削除ここまで) (削除) 22 (削除ここまで) 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
-
\$\begingroup\$ You can save 2 bytes with
imag({iAns,Ansin the last line (byiI mean the complex number i). \$\endgroup\$Misha Lavrov– Misha Lavrov2017年10月14日 20:57:14 +00:00Commented Oct 14, 2017 at 20:57 -
1\$\begingroup\$ And I guess then one more byte by just combining the two lines into
imag({i,1}(A+Bi)^C. \$\endgroup\$Misha Lavrov– Misha Lavrov2017年10月14日 21:01:20 +00:00Commented Oct 14, 2017 at 21:01 -
1\$\begingroup\$ Rules have changed, now you can take input and return complex numbers, if that is of any help. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年10月15日 12:11:33 +00:00Commented Oct 15, 2017 at 12:11
6502 machine code subroutine, (削除) 199 (削除ここまで) (削除) 187 (削除ここまで) 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 6ドルF
D0 36 BNE .mult16 ; branch to 16bit multiplication
.mult5:
18 CLC ; calculate sum (a*d) + (b*c)
A5 6D LDA 6ドルD
65 65 ADC 65ドル
85 28 STA 28ドル ; and store to imaginary component of result
A5 6E LDA 6ドルE
65 66 ADC 66ドル
85 29 STA 29ドル
A5 4B LDA 4ドルB ; load temporary result (a*c) - (b*d)
85 26 STA 26ドル ; and store to real component of result
A5 4C LDA 4ドルC
85 27 STA 27ドル
50 CF BVC .exploop ; next exponentiation step
.mult3:
38 SEC ; calculate difference (a*c) - (b*d)
A5 6D LDA 6ドルD
E5 65 SBC 65ドル
85 4B STA 4ドルB ; and store to temporary location
A5 6E LDA 6ドルE
E5 66 SBC 66ドル
85 4C STA 4ドルC
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 6ドルF ; 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ドル
90 0D BCC .m16_noadd ; no carry -> nothing to add
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
.m16_noadd:
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 6ドルF
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 6ドルD
A5 66 LDA 66ドル
85 6E STA 6ドルE
24 6F BIT 6ドルF ; 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 6ドルF ; 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 6ドルF ; 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 #0ドルd ; 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○しろまる⊢
a is left argument, b is right argument, and c via input prompt.
Returns a complex number in the format dJe.
-
1\$\begingroup\$ Rules have changed, now you can take input and return complex numbers, if that's of any help. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年10月15日 12:13:17 +00:00Commented Oct 15, 2017 at 12:13
-
\$\begingroup\$ Now, you can input and output complex numbers directly (according to rules). So the answer is simply the
*function (or, taking input from stdin and outputting to stdout, we have:⎕*⎕). \$\endgroup\$Sohang Chopra– Sohang Chopra2021年10月12日 11:34:21 +00:00Commented Oct 12, 2021 at 11:34
MATL, 1 byte
^
Inputs are a+jb, c.
Old version: non-complex input and output, 8 bytes
J*+i^&Zj
Input order is b,a, c.
Explanation
J Push imaginary unit
* Multiply by implicit input b
+ Add implicit input a
i Take input c
^ Power
&Zj Push real and imaginary parts. Implicitly display
-
\$\begingroup\$ Multiply by implicit input b - Add implicit input b. Did you mean a in either of those? \$\endgroup\$Mr. Xcoder– Mr. Xcoder2017年10月14日 17:13:04 +00:00Commented Oct 14, 2017 at 17:13
-
\$\begingroup\$ @Mr.Xcoder Yes, thanks. Corrected \$\endgroup\$Luis Mendo– Luis Mendo2017年10月15日 02:42:05 +00:00Commented Oct 15, 2017 at 2:42
-
\$\begingroup\$ 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. \$\endgroup\$Steven H.– Steven H.2017年10月15日 11:48:41 +00:00Commented Oct 15, 2017 at 11:48
-
\$\begingroup\$ @StevenHewitt Thanks! Edited now \$\endgroup\$Luis Mendo– Luis Mendo2017年10月15日 12:36:53 +00:00Commented Oct 15, 2017 at 12:36
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.
Ungolfed version with comments
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.
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.
Perl 6, (削除) 29 26 20 19 (削除ここまで) 11 bytes
{$_=($^a+$^b*i)**$^c;.re,.im}
{(($^a+$^b*i)**$^c).reals}
((*+* *i)** *).reals
((*+* *i)***).reals
With the change of output restrictions it can be further reduced:
(*+* *i)***
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
-
\$\begingroup\$ You can now do
(*+* *i)***. \$\endgroup\$totallyhuman– totallyhuman2017年10月15日 12:19:31 +00:00Commented Oct 15, 2017 at 12:19
R, 25 bytes
simplest - since outputting complex is allowed.
function(a,b,c)(a+b*1i)^c
-
\$\begingroup\$ you can simplify your code by just taking a complex number
zas input - then function body is simplyz^c. \$\endgroup\$Sohang Chopra– Sohang Chopra2021年10月12日 11:36:01 +00:00Commented Oct 12, 2021 at 11:36
Explore related questions
See similar questions with these tags.