by Arne Sommer

# Boxed Primes with Raku

 Published 30. June 2023.

This is my response to The Weekly Challenge #223.

## Challenge #223.1: Count Primes

You are given a positive integer, `\$n`.

Write a script to find the total count of primes less than or equal to the given integer.

Example 1: ```Input: \$n = 10 Output: 4 Since there are 4 primes (2,3,5,7) less than or equal to 10. ``` Example 2: ```Input: \$n = 1 Output: 0 ``` Example 3: ```Input: \$n = 20 Output: 8 Since there are 4 primes (2,3,5,7,11,13,17,19) less than or equal to 20. ```

This is basically a one liner, shown here with the added crash bang line and error checking on the input.

File: count-primes ```#! /usr/bin/env raku unit sub MAIN (UInt \$n where \$n > 0); #  say (1 .. \$n).grep({ .is-prime }).elems; #  ```

 Ensure a positive (`where \$n > 0`) integer (`UInt` gives an usigned int, which includes zero, but the other part fixes that).

 Start with all the possible integers (`1 .. \$n`), disregarding the fact that 2 is the lowest prime (so we could have started there, instead of at 1). Then we use `grep` to collect only the values that are prime (with `.is-prime`). We are interested in the number of primes, not the values themselves, so we apply `.elems` to do just that, before printing the result.

See docs.raku.org/routine/is-prime for more information about `is-prime`.

Running it:

```\$ ./count-primes 10 4 \$ ./count-primes 1 0 \$ ./count-primes 20 8 ```

Looking good.

Some more, just for fun:

```\$ ./count-primes 1_000_000 78498 \$ ./count-primes 2_000_000 148933 \$ ./count-primes 3_000_000 216816 \$ ./count-primes 4_000_000 283146 \$ ./count-primes 5_000_000 348513 ```

## Challenge #223.2: Box Coins

You are given an array representing box coins, `@box`.

Write a script to collect the maximum coins until you took out all boxes. If we pick `box[i]` then we collect the coins `\$box[i-1] * \$box[i] * \$box[i+1]`. If `\$box[i+1]` or `\$box[i-1]` is out of bound then treat it as `1 coin`.

Example 1: ```Input: @box = (3, 1, 5, 8) Output: 167 Step 1: pick box [i=1] and collected coins 3 * 1 * 5 => 15. Boxes available (3, 5, 8). Step 2: pick box [i=1] and collected coins 3 * 5 * 8 => 120. Boxes available (3, 8). Step 3: pick box [i=0] and collected coins 1 * 3 * 8 => 24. Boxes available (8). Step 4: pick box [i=0] and collected coins 1 * 8 * 1 => 8. No more box available. ``` Example 2: ```Input: @box = (1, 5) Output: 10 Step 1: pick box [i=0] and collected coins 1 * 1 * 5 => 5. Boxes available (5). Step 2: pick box [i=0] and collected coins 1 * 5 * 1 => 5. No more box available. ```

We are looking for the maximum. This is best achieved by eliminating the smallest numbers first, so that can multiply even larger numbers in future steps.

This approach works until we have 3 boxes left, and this step should give us the largest contribution to the grand total (as we now have the threee largest numbers in the input; just as in step 2 for the second example).

Now we pick the one in the middle (so that we can multiply all of the three remaining numbers together). Then we have two. it does not matter which one we pick for this step by itself, as we multiply them together. But we should pick the lowest one, so that the highest one can come in at the very end (when we have only one).

File: box-coins ```#! /usr/bin/env raku unit sub MAIN (*@box where @box.elems > 0 #  && all(@box) ~~ UInt && all(@box) > 0, :v(:\$verbose)); my \$coins = 0; #  while (@box.elems) #  { my \$i = 0; #  if @box.elems > 3 #  { \$i = smallest-index(@box); # [5a] } elsif @box.elems == 3 #  { \$i = 1; # [6a] } elsif @box.elems == 2 #  { \$i = 1 if @box < @box; # [7a] } print ":Box: @box[] | I:\$i" if \$verbose; my \$left = \$i > 0 ?? @box[\$i -1] !! 1; #  my \$curr = @box[\$i]; #  my \$right = \$i < @box.end ?? @box[\$i +1] !! 1; #  my \$add = \$left * \$curr * \$right; #  \$coins += \$add; #  @box.splice(\$i,1); #  say " -> box: @box[] | coins: \$add | total: \$coins" if \$verbose; } say \$coins; #  sub smallest-index (@list) #  { my \$index = 0; #  my \$val = @list; #  for 1 .. @list.end -> \$i #  { if @list[\$i] < \$val #  { \$index = \$i; # [19a] \$val = @list[\$i]; # [19b] } } return \$index; #  } ```

 At least one value (box); and the values must be positive integers.

 The sum (number of coins) obtained after taking out all the boxes.

 As long as we have boxes left.

 The i (index) to use, with 0 as the default value. (This is used on the very last item, as the if block [5-7] does not apply.)

 More than three elements? Choose the index of the lowest number [5a].

 Exactly three elemets? Choose the middle one. [6a]

 Exactly two elements? Choose the index of the lowest number [7a].

 The left value, or 1 if it is out of bounds.

 The current value.

 The right value, or 1 if it is out of bounds.

 Multiply the three values together.

 Add the new value to the total number of coins.

 Remove the chosen value from the list (with `splice`).

See docs.raku.org/routine/splice for more information about `splice`.

 Print the total number of coins.

 Get the index of the lowest value in the supplied array.

 Starting at index 0.

 And the value found at index 0.

 Iterate over the indices of the rest of the array (i.e. excluding the initial value).

 Is the new value lower than the previously lowest value? If so, save the index [19a] and value [19b]. (Note that multiple instances of the same lowest value will give us the index of the first one.)

 Return the index of the lowest value.

Running it:

```\$ ./box-coins 3 1 5 8 167 \$ ./box-coins 1 5 10 ```

Looking good.

With verbose mode:

```\$ ./box-coins -v 3 1 5 8 :Box: 3 1 5 8 | I:1 -> box: 3 5 8 | coins: 15 | total: 15 :Box: 3 5 8 | I:1 -> box: 3 8 | coins: 120 | total: 135 :Box: 3 8 | I:0 -> box: 8 | coins: 24 | total: 159 :Box: 8 | I:0 -> box: | coins: 8 | total: 167 167 \$ ./box-coins -v 1 5 :Box: 1 5 | I:0 -> box: 5 | coins: 5 | total: 5 :Box: 5 | I:0 -> box: | coins: 5 | total: 10 10 ```

And that's it.