Created
September 17, 2021 11:39
-
-
Save salva/c667f7d6e498754fcfa963bed4f624f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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