## Advent Calendar - December 6, 2019

Friday, Dec 6, 2019| Tags: Raku

The gift is presented by Arne Sommer. Today he is talking about his solutions to Task #1: Pythagoras Pie Puzzle** of “The Weekly Challenge - 016”**.

#### 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. Write a script that figures out which guest gets the largest piece of pie.

We can start with the numbers (the size of the pieces), which is suitable job for a sequence with «gather/take»:

pythagoras-pie-all

``````my \$pythagoras-pie := gather  # [1]
{
my \$remainder = 100;        # [2]
my \$position  = 1;          # [3]
loop                        # [4]
{
my \$part = \$remainder * \$position / 100; # [5]
\$remainder -= \$part;                     # [6]
\$position++;                             # [7]
take \$part;                              # [8]
}
}

say "{ \$_ + 1 } ->  \$pythagoras-pie[\$_]%" for ^100; [9]
``````

\

### [9] Print the 100 first values from the sequence. (Any values after this are all zero.)

Running it (with just some of the lines shown):

``````\$ perl6 pythagoras-pie-all
1 ->  1%
2 ->  1.98%
3 ->  2.9106%
4 ->  3.764376%
5 ->  4.5172512%
6 ->  5.149666368%
7 ->  5.64746745024%
8 ->  6.002451118541%
9 ->  6.21253690768973%
10 ->  6.281565095552947%
11 ->  6.21874944459741773%
12 ->  6.037840369845492849%
13 ->  5.75607448591936984904%
14 ->  5.392999018345995%
15 ->  4.969263381190237%
...
96 ->  3.7330486177577654e-34%
97 ->  1.5087738163437654e-35%
98 ->  4.572984556753267e-37%
99 ->  9.239295328950519e-39%
100 ->  9.332621544394437e-41%
``````

We can see that the answer is 10; i.e. guest number 10 gets the largest piece of cake. But the task was for the program to figure it out, so let’s do just that.

Finding the largest value is easy, as long as we chop off the sequence after the first 100 elements. (The rest of them, ad infinitum, are zero. Trying to access all elements in an infinite list is not a good idea.)

``````say \$pythagoras-pie[^100].max; # -> 6.281565095552947
``````

But this doesn’t tell us the position (or index), and thus which guest.

pythagoras-pie \

``````my \$pythagoras-pie := gather
{
my \$remainder = 100;
my \$position  = 1;
loop
{
my \$part = \$remainder * \$position / 100;
\$remainder -= \$part;
\$position++;
take \$part;
}
}
\
my \$largest = \$pythagoras-pie[^100].max;                          # [1]

for (^100).grep({ \$pythagoras-pie[\$_] == \$largest }) -> \$guest    # [2]
{
say "Guest #{ \$guest + 1 } got the largest piece of cake ({ \$largest }%).";
}                # [3]
``````

\

\

# Running it:

``````\$ perl6 pythagoras-pie
Guest #10 got the largest piece of cake (6.281565095552947%).
``````

\

If you have any suggestion then please do share with us perlweeklychallenge@yahoo.com.