RosettaCodeData/Task/Gray-code/Picat/gray-code.picat

27 lines
632 B
Plaintext

go =>
foreach(I in 0..2**5-1)
G = gray_encode1(I),
E = gray_decode1(G),
printf("%2d %6w %2d %6w %6w %2d\n",I,I.to_binary_string,
G, G.to_binary_string,
E.to_binary_string, E)
end,
nl,
println("Checking 2**1300:"),
N2=2**1300,
G2=gray_encode1(N2),
E2=gray_decode1(G2),
% println(g2=G2),
% println(e2=E2),
println(check=cond(N2==E2,same,not_same)),
nl.
gray_encode1(N) = N ^ (N >> 1).
gray_decode1(N) = P =>
P = N,
N := N >> 1,
while (N != 0)
P := P ^ N,
N := N >> 1
end.