<>, Invalid27 + 3 for -v = 30 bytes =============
Edit : I might be missing something, butHere is a not-uniform-at-all solution where I think it's just impossible ifmod N isn't a multiplethe sum of 2,3 or 4 (the number15876 random picks of exits from a0 or 1 x
symbol). If someone knows the maths to make it work I'll:
0"~":*>:?vr%0=n;
1-$1+$^-1x
N must be glad to implement itinput on the stack with !-v flag, output is 0 for falsey and 1 for truthy.
Let's hope you will still be interested by an answerA much smarter and uniform solution that work for 1/2^N instead :
4{:?!v1-}:">"$2p:"x"$3p:"^"$4p1+:">"$3p1+!
^1<
0n;
1n;>
Input is to be input directly on the stack with -v to avoid the tedious integer parsing. Truthey output will be 1, falsey will be 0. So forFor an input 3 you've got 1/8 chances of getting 1 and 7/8 of getting 0.
Explanation :
I append as much x
as needed on the 4th line and surround them with directions so there is only two ways out of the x
: either the falsey output or the next x
. If all x
go in the right direction, the last one will route to the truthy output.
For example for N=5, the final codespace is the following :
4{:?!v1-}:">"$2p:"x"$3p:"^"$4p1+:">"$3p1+!
^1<
0n; > > > > >
1n;>x>x>x>x>x>
^ ^ ^ ^ ^