Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created October 3, 2019 10:54
Show Gist options
  • Select an option

  • Save codecakes/7c7b797192ca4111ec47fe43297438a5 to your computer and use it in GitHub Desktop.

Select an option

Save codecakes/7c7b797192ca4111ec47fe43297438a5 to your computer and use it in GitHub Desktop.
XOR of all numbers from 1 to N.
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