blob: 14c25126a5213bdf662ff3c86384f0a8ce44521b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
#!/usr/bin/env perl6
use v6.d;
# I know there's a mathematical model for this (I saw a video on it)
# But I can't remember it so the default version will just calculate it.
#| Calculate the survior of the swordsmen suicide pact
multi sub MAIN(
UInt $swords = 50, #= Number of swordsmen (default 50)
) {
my @men = [1..$swords];
while ( @men.elems > 1 ) {
my ( $alive, $dead ) = @men.splice(0,2);
@men.push($alive);
}
say "Survivor of $swords is number {@men[0]}";
}
# So after doing that I got it.
# Find p where p ** 2 < s (swordsmen)
# The survior is the nth odd number where n = s - p
#| Calculate mathematically
multi sub MAIN(
"math",
UInt $swords = 50, #= Number of swordsmen
) {
my $low-power = (1,* * 2...*).first(* > $swords) div 2;
say "Survivor of $swords is number {(1,3,5...*)[$swords - $low-power]}";
}
|