27
1
The Challenge
In as few characters as possible, find the value of i^n, given n, a positive integer greater than 0. This should be outputted as a String.
For those that don't know, i is defined such that i^2=-1. So:
- i^1=i
- i^2=-1
- i^3=-i
- i^4=1
This then repeats..
Rules
- If your language supports complex numbers, don't use any functions or arithmetic that could work this out.
- Floating point inaccuracies are fine for answers that would return decimals anyway, but integer inputs should give exact results
Bonus Points
-5 if you can work the value out where n is also negative
-15 if you can work out the value for any real number (this bonus includes the -5 from the above bonus)
Good luck!
1In what format do we return exactly? Through function output or stdout? – proud haskeller – 2014-11-21T15:59:36.040
@proudhaskeller The tag wiki lists defaults for that. Unless specified otherwise, functions and programs are fine, input via function argument, STDIN or command-line argument and output via STDOUT or function return value. Functions do not have to be named. – Martin Ender – 2014-11-21T16:00:54.357
1@MartinBüttner but if i choose function output, how should the output should be formatted/stored without native complex numbers in my language? – proud haskeller – 2014-11-21T16:02:50.550
If you support any real number you can output in any valid complex form. So just 1 would be valid and so would 1+0i. – Kezz101 – 2014-11-21T16:31:00.210
Well I think that floating point inaccuracies are fine for answers that would return decimals anyway, but integer inputs should give exact results. (This is my first question posed so if you disagree let me know!) – Kezz101 – 2014-11-21T17:23:20.430
Okay, I shall do! – Kezz101 – 2014-11-21T17:42:07.677
Do we require the use of floating point integers? – Beta Decay – 2014-11-21T17:58:15.357
16@BetaDecay What are floating point integers? o.O – Martin Ender – 2014-11-21T18:04:23.263
2@MartinBüttner Haha wrong word :/ Floating point number then – Beta Decay – 2014-11-21T18:17:18.337
Common Lisp, (26 characters - 15 = 11):
(lambda(n)(expt #C(0 1)n))
. Unfortunately, this is against the rules :-( – coredump – 2014-11-21T19:12:51.323I feel so stupid. I only just realized that python has a built in
complex
type. – Justin – 2014-11-22T06:03:00.583i is not defined as the square root of -1. Rather, it is defined such that i^2 = -1, which is not the same, since one can choose plus or minus the root of -1. – user1997744 – 2014-11-22T18:12:57.867
@user1997744 True, I'll edit it now although I doubt it'll make any difference.. – Kezz101 – 2014-11-22T18:16:22.270
mod 4 and a lookup. This doesn't seem very interesting. – Tim Seguine – 2014-11-24T13:58:48.233
@user1997744 in all fairness, it is possible to define square root that way as well, making it unique by taking a branch cut. – Tim Seguine – 2014-11-24T14:01:08.783