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

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

See The Solution Submitted by Praneeth    
No Rating

Previous (in Geometry) : Primitive Pythagorean Triangles
    Next (in Calculus) : Find The Value
Comments: ( Back to comment list | You must be logged in to post comments.)
Question 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

Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (7)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2014 by Animus Pactum Consulting. All rights reserved. Privacy Information