Find i^n, given n
Ruby, score -2
(13 bytes, -15 bonus)
Features include: no rounding errors! (if you pass the input as a Rational)
posted by the author, Kezz101
If you support any real number you can output in any valid complex form.
Negatives scores make my adrenaline rush forth. Thus the rules
get abused are made use of to achieve this noble goal.
Creates an anonymous function and outputs an array with 2 entries representing a complex number in polar form (angular unit: degrees).
CJam, 12 characters - 5 = 7
Test it here.
Supports negative inputs.
1 "Push 1."; 'i "Push the character i."; W "Push -1."; "-i" "Push the string -i."; ] "Wrap all four in an array."; li "Read STDIN and convert to integer."; = "Access array. The index is automatically taken module the array length.";
The result is printed automatically at the end of the program.
22 20 19 characters - 15 = 4
This is an anonymous function, which you can use like
(Or assign it to
f say, and then do
Supports reals and gives exact results for integer input.
Note that the
i is not Mathematica's complex i (which is
I). It's just an undefined variable.
Also, despite the order of the expression, Mathematica will reorder the output into
Python 2 - (24-5)=19
Most credit belongs to @user2357112, I just golfed his answer from the comments on this answer a bit more.
Explanation: Starts at the index
n%4 in the string
'1i--'. Then iterates backwards in steps of two over each letter in the string. So, for example,
n=6 would start at index 2, the first
-, then skip the
i and take the
1, to return
@xnor pointed out a same-length solution:
Pyth - (14-5)=9
I can only seem to get 14, no matter how I try to reverse/slice/etc. :'(
Which is essentially the same as the above python answer, but in 2 steps, because pyth doesn't support the full indexing options of python. Try it online.
I'm going to go have a talk with isaacg about Pyth indexing ;)