New answers tagged

1

Ruby, 41 40 bytes f=->n{n>0?f[(n-w=n%2*(2-n%4))/2]+[w]:[]} Try it online!


1

C (clang), 103 \$\cdots\$ 96 94 bytes m;i;f(n){int r[n];for(i=!n,n+=*r=m=n/2;n;n/=2,m/=2)r[i++]=n%2-m%2;for(;i--;)putchar(r[i]+49);} Try it online! Saved 2 bytes thanks to ceilingcat!!! Inputs positive integer \$n\$. Outputs \$n\$ converted to its non-adjacent form using \$2\$ for \$1\$s, \$1\$ for \$0\$s, and \$0\$ for \$-1\$s.


2

05AB1E, 21 15 bytes 0‚¬2÷+2вí`(0ζRO -6 bytes thanks to @m90, thanks to his observation that & c only changes [1,1] to [0,0], which doesn't matter when we're subtracting them. Try it online or verify some more test cases. Explanation: 0‚ # Pair the (implicit) input with 0 ¬ # Get the first item of this pair (the input) without popping ...


1

Vyxal, 4 bytes b›3β Try it Online! b # Binary › # Increment 3β # From base 3


2

Python 3, 176 103 bytes Literal translation of Prodinger's algorithm. By no means the shortest python 3 answer that should be attainable. it's been a while on here so looking forward to getting back into it! x=int(input()) h=x>>1 t=h+x c=h^t p=bin(t&c) m=bin(h&c) n=[] j=len(p)-2 k=len(m)-2 for i in range(j):n.append(int(p[i+2])) for i in range(...


4

JavaScript, 35 bytes f=n=>n?f(n+n%4/3>>1)+'OPON'[n%4]:'' Try it online! Output O for 0, P for 1, N for -1. -2 bytes by Arnauld. Alternatively, as suggested by Arnauld, using 1 for 1, 2 for 0, 0 for 0, and, 3 for -1: JavaScript, 31 bytes f=n=>n?f(n+n%4/3>>1)+n%2*n%4:'' Try it online!


6

Jelly, 13 9 bytes ;0+HBU_/U Try it online! -4 bytes (^/&$ removed) thanks to @m90's observation that we don't need to clear shared 1 bits. Look ma, no Unicode! Literal translation of Prodinger's algorithm. x3 goes before xh, and U is used twice to align the binary representations of the two.


6

K (ngn/k), 19 12 bytes -/'2\-2!3 1* Try it online! Uses Prodinger's algorithm on Wikipedia: Input x Output np, nm xh = x >> 1; x3 = x + xh; c = xh ^ x3; np = x3 & c; nm = xh & c; And then the last three steps are not used because the & c part of np and nm erases the 1 bits where both are 1, but we're doing -/' so it doesn't matter. (...


Top 50 recent answers are included