I decided to take a shot at some tasks from the Perl Weekly Challenge.
At a party a pie is to be shared by 100 guest. The first guest gets 1% of the pie, the second guest gets 2% of the remaining pie, the third gets 3% of the remaining pie, the fourth gets 4% and so on.
sub eat {
state $cake = 100;
given $cake * $^share/100 {
$cake -= $_;
$_;
}
}
.put for (1..100).map(&eat).pairs.sort(-*.value)[^5];
9 6.281565095552947 10 6.21874944459741773 8 6.21253690768973 11 6.037840369845492849 7 6.002451118541
The 10th guest gets the largest share of the pie at 6.28%.
Create a script to demonstrate Ackermann function. The Ackermann function is defined as below, m and n are positive number:
A(m, n) = n + 1 if m = 0 A(m, n) = A(m - 1, 1) if m > 0 and n = 0 A(m, n) = A(m - 1, A(m, n - 1)) if m > 0 and n > 0
proto A($m, $n) { %.{"$m,$n"} //= {*} }
multi A(0, $n) { $n + 1 }
multi A($m, 0) { A($m - 1, 1) }
multi A($m, $n) { A($m - 1, A($m, $n - 1)) }
sub MAIN(UInt $m, UInt $n) { say A($m, $n) }
An anonymous hash is used to cache already computed values within the proto.