Created
October 3, 2019 10:54
-
-
Save codecakes/7c7b797192ca4111ec47fe43297438a5 to your computer and use it in GitHub Desktop.
XOR of all numbers from 1 to N.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def xorN(n): | |
| '''Xor of all numbers from 1 to n.''' | |
| rem = n%4 | |
| if rem ==0: | |
| return n | |
| xord = functools.reduce(lambda x,y: x^y, range(1, rem+1)) | |
| return xord if xord < 2 else n+1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment