Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The simplest formula from the OEIS page seems to be

  a(4n)=4n,

  a(4n+1)=1,

  a(4n+2)=4n+3,

  a(4n+3)=0.
Once you have the formula in front of you, it’s easy to prove it by induction. A branchless implementation of this function with no multiplies or divides:

  f(x)=(x^(x&1-1))+(((x+1)&2)>>1)


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: