The Rundown

Given any input x and y, perform a complex operation, and print a corresponding result.

How your program should work

  1. Given an input x and y in the form z = x+yi, find zi-z

  2. If the absolute real value of zi-z is larger than the absolute imaginary part, print the real part; vice versa for the other way around. If both values are equal, print one of the values.


x: 2
y: 0


z = 2
z^(i-z) ~= 0.192309 + 0.159740i

Since the real part has a larger absolute value than the imaginary part, the program returns


More examples

z = 1+i >> 0.5
z = i >> 1
z = 0.5 >> 1.08787
z = -2+8i >> 2.22964E7
z = -10i >> 3.13112E7
  • 10
    \$\begingroup\$ Pro-tip: get rid of the bonus! \$\endgroup\$ Apr 17, 2017 at 21:44
  • 3
    \$\begingroup\$ Things to avoid when writing challenges - bonuses in code-golf \$\endgroup\$
    – mbomb007
    Apr 17, 2017 at 21:47
  • 7
    \$\begingroup\$ Raising a complex numbers to a complex power is discontinuous and depends on the branch cut used. Can you specify that? Though I guess everyone will just be using built-in math operations and those probably all use the same convention. \$\endgroup\$
    – xnor
    Apr 17, 2017 at 22:16
  • 2
    \$\begingroup\$ Does "larger" mean to pick the value with greatest absolute value, rather than (what most have assumed) to pick the maximal value? A test case of -2+i could be used for that (z^(i-z)=3-4i so 3>-4 vs abs(-4)>abs(3)). \$\endgroup\$ Apr 18, 2017 at 5:44
  • 5
    \$\begingroup\$ The "absolute value" clarification/change has invalidated most of the answers. \$\endgroup\$
    – xnor
    Apr 18, 2017 at 22:32

15 Answers 15


Jelly, 8 11 bytes

Thanks Johnathan Allan for updating the answer with the rules change.


Try it online!

ı_*@        z^(i-z)
    µ       new monadic link
     Ċ,Ḟ    pair real and imaginary parts
        AÞṪ sort by absolute value and take last value
  • \$\begingroup\$ Make it ı_*@µĊ,ḞAÞṪ and you may well have the only valid entry (given the change to require the maximal value in absolute terms, such that, for example, -2+1j returns -4.0 rather than 3.0). \$\endgroup\$ Apr 19, 2017 at 6:47

Python 2, 45 bytes

def f(z):z=z**(1j-z);print max(z.real,z.imag)

Try it online - all test cases

Programming languages often use j instead of i. That is the case in Python. See this SO question for more information on why.


Mathematica, 21 22 bytes

Edit: Thanks to JungHwan Min for saving 3 btyes


Pure function which expects a complex number as an argument. If an exact number is passed, then an exact number will be returned (e.g. 1/2 gives Sqrt[2] Cos[Log[2]]). The problem spec was edited after I posted my solution to specify that the absolute value should be used. The best I can come up with for that is MaximalBy[ReIm[#^(I-#)],Abs][[1]]& or Last@MaximalBy[Abs]@ReIm[#^(I-#)]&, both 34 bytes.

  • 1
    \$\begingroup\$ Max does not need to be the head. It returns the max value no matter how deep the input List is (e.g. Max[1, {2, {3}}] returns 3). Also, the question only specifies that you print the values, so I don't think you would need N: Max@ReIm[#^(I-#)]& would work. \$\endgroup\$ Apr 18, 2017 at 1:59

Octave, 29 bytes

@(z)max(real(z^(i-z)./[1 i]))

This defines an anonymous function. It works in MATLAB too.

Try it online!


Element-wise dividing (./) the number z^(i-z) by the array [1 i] and taking the real part gives an array with the real and imaginary parts of z^(i-z).


MATL, 10 bytes


Try it online! Or verify all test cases.


Consider input -2+8i as an example.

J     % Push i (imaginary unit)
      % STACK: i
y     % Implicit input. Duplicate from below
      % STACK: -2+8i, i, -2+8i
-     % Subtract
      % STACK: -2+8i, 2-7i
^     % Power
      % STACK: 3168271.58+22296434.47i
&Zj   % Real and imaginary parts
      % STACK: 3168271.58, 22296434.47
h     % Concatenate
      % STACK: [3168271.58 22296434.47]
X>    % Maximum. Implicitly display
      % STACK: 22296434.47

TI-BASIC, 40, 32, 31 29 bytes

Saved a byte thanks to @Conor O'Brien

Z^(i-Z→A                   #Perform operation, store as A, 8 bytes
:real(A)>imag(A            #Test if real part is greater than imaginary, 9 bytes
:Ansreal(A)+imag(Anot(Ans  #Determine output and print, 12 bytes

Takes input as a complex number on the Z variable.

TI-BASIC uses its own encoding, you can find it here.


Pyth, 16 Bytes


Perl 6, 24 bytes


$_ is the possibly complex argument; $_ ** (i - $_) is the expression to be computed; .reals is a Complex method that returns a list of the real and imaginary parts; and finally .max returns the larger of the two.


C (GCC), 93 79 + 4 (-lm) = 97 83 bytes

Saved 14 bytes thanks to @ceilingcat!

float f(_Complex z){z=cpow(z,csqrt(-1)-z);return cimag(z)>creal(z)?cimag(z):z;}

Including the header complex.h is longer than that ¯\_(ツ)_/¯

Try it online!

  • \$\begingroup\$ Why +4 bytes? I count 3, -, l, and m. \$\endgroup\$
    – Riker
    Apr 18, 2017 at 3:44
  • \$\begingroup\$ @Riker normal compiling is gcc file.c -o exe, so this flag adds 4 bytes: space, -, l, and m. (At least that's how I see it counted when compiling.) \$\endgroup\$
    – betseg
    Apr 18, 2017 at 4:45
  • \$\begingroup\$ @ceilingcat oh didn't know that was possible. Thanks! \$\endgroup\$
    – betseg
    Apr 19, 2017 at 9:01
  • \$\begingroup\$ 54 bytes \$\endgroup\$
    – ceilingcat
    Dec 9, 2018 at 9:29

Ruby, 25 35 bytes

EDIT: Fixed to comply with new absolute value rule.


Try it online!

This creates an anonymous function.


TI-Basic, 19 16 bytes


real( and imag( are two-byte tokens.

Run with 5+3i:prgmNAME (5+3i being the argmuent, NAME being the program name.)


APL (Dyalog Unicode), 18 bytes

18 bytes (https://github.com/abrudz/SBCS/)
28 bytes (UTF-8)

⍵ is a complex number ajb = a+bi

{⌈/9 11∘.○⍵*0j1-⍵}

Try it online!


R, 38 bytes


Anonymous function. Takes a (possibly) complex number z, takes it to the specified power, and then returns the max of the Real and Imaginary parts.


Axiom, 60 bytes

f(z:Complex Float):Float==(y:=z^(%i-z);max(real(y),imag(y)))

test code and results; i follow as the other the precedent version of the question...

(28) -> [[k,f(k)] for k in [1+%i,%i,1,-2+8*%i,-10*%i]]
   [[1.0 + %i,0.5], [%i,1.0], [1.0,1.0],
    [- 2.0 + 8.0 %i,22296434.4737098688 53],
    [- 10.0 %i,31311245.9804955291 66]]

C# - 189 Bytes

double f(double x, double y){double r,t,m,c;r=Math.Sqrt(x*x+y*y);t=Math.Atan2(y,x);m=Math.Pow(r,-x)*Math.Exp(y*t-t);c=Math.Cos((1-y)*Math.Log(r)-t*x);return m*(2*c*c<1?Math.Sqrt(1-c*c):c);}


double f(double x, double y){
double r, t, m, c;
r = Math.Sqrt(x * x + y * y);
t = Math.Atan2(y, x);
m = Math.Pow(r, -x) * Math.Exp(y * t - t);
c = Math.Cos((1 - y) * Math.Log(r) - t * x);
return m * (2 * c * c < 1 ? Math.Sqrt(1 - c * c) : c); }

Explanation: Decided not to use any Complex libraries.

$$ \begin{align} z & = x + iy \\ & = r e^{it} \\ z^{i-z} & = (re^{it})^{(-x +i(1-y))} \\ & = r^{-x} r^{i(1-y)} e^{-xit} e^{t(y-1)} \\ & = r^{-x} e^{t(y-1)} e^{i((1-y)\ln(r)-xt)} \text{ (as }r^i = e^{i\ln(r)}\text{)} \end{align} $$

Let this be equal to \$me^{ia}\$ where

$$m = r^{-x} e^{t(y-1)}$$ $$a = (1-y)\ln(r)-xt$$

Then \$\Re(z^{i-z})=m \cos a\$ and \$\Im(z^{i-z})=m \sin a\$

The maximum absolute value can be determined by the \$\cos a\$ and \$\sin a\$ terms, with these being equal at \$\frac{1}{\sqrt{2}}\$ (hence the test \$2c^2 < 1\$).

As mentioned, raising to a complex exponent is dependent on choosing a particular branch cut ( e.g. \$z = 1\$ could be \$e^{i\pi}\$ or \$e^{3i\pi}\$ - raising this to \$i\$ gives a real part of \$e^{-\pi}\$ or \$e^{-3\pi}\$ respectively), however, I have just used the convention of \$t \in [0,2\pi)\$ as per the question.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.