All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars
 perplexus dot info

 Single Line Expression (Posted on 2008-01-27)
Give a single line "C type" expression to evaluate (-1)k using only +,-,* and % operators.

 See The Solution Submitted by Praneeth No Rating

Comments: ( Back to comment list | You must be logged in to post comments.)
 re: Solution | Comment 2 of 5 |
(In reply to Solution by Kurious)

And how does it evaluate when k is not an integer?

(-1)k,
where k=1/n : n is an odd positive integer > 1, is equal to -1

1-k%2*2,
where k=1/n : n is an odd positive integer > 1, is not equal to -1

Also note: where n is an even positive integer, (-1)k becomes an imaginary number.

Other considerations are where k = m/n, such that m/n is a reduced fraction.  Many applications have trouble providing anything other than an error message or like.
How can we then evaluate (-1)k using only the operators + (addition), - (subtraction), * (multiplication) and % (modulo)?

Edited on January 28, 2008, 1:10 pm
 Posted by Dej Mar on 2008-01-27 22:17:06

 Search: Search body:
Forums (0)