Skip to content

Instantly share code, notes, and snippets.

@salva
Created September 17, 2021 11:39
Show Gist options
  • Save salva/c667f7d6e498754fcfa963bed4f624f1 to your computer and use it in GitHub Desktop.
Save salva/c667f7d6e498754fcfa963bed4f624f1 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
my $n = shift;
my @offsets;
my $offset = 2 - ($n & 1);
my $size = $n;
while ($size > $offset) {
unshift @offsets, $offset;
$size -= $offset;
$offset = (3 - $size % 3) % 3;
$size = int(($size + 2) / 3);
}
my $i = $size - 1;
for my $offset (@offsets) {
$i = $i * 3 + $offset;
}
print(($i + int($n/2)) % $n + 1, "\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment