by Arne Sommer

# Primary Five with Raku

 Published 10. July 2022.

This is my response to The Weekly Challenge #172.

## Challenge #172.1: Prime Partition

You are given two positive integers, `\$m` and `\$n`.

Write a script to find out the `Prime Partition` of the given number. No duplicates allowed.

For example, ```Input: \$m = 18, \$n = 2 Output: 5, 13 or 7, 11 Input: \$m = 19, \$n = 3 Output: 3, 5, 11 ```

`\$m` is the number to partition, and `\$n` is the number of primes we want in the partition. See e.g. mathworld.wolfram.com/PrimePartition.html for a detailed explanation.

File: prime-partition ```#! /usr/bin/env raku unit sub MAIN (Int \$m where \$m > 0, Int \$n where \$n > 0, :a(\$all)); #  my \$primes := (2 .. \$m).grep( *.is-prime ); #  for \$primes.combinations(\$n) -> @combination #  { my \$sum = @combination.sum; #  if \$sum == \$m #  { say @combination.join(", "); #  last unless \$all; #  } } ```

 Specify the two values. Also note the «-all», which will be discussed in .

 Get the primes, up to (and including, even if that is excessive) the original number. (But this will give us the number itself, if we go for `\$n = 1`.)

 Then we use `combinations(\$n)` to get all the possible combinations of values in the original list - of size `\$n`, and iterate over them.

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

 Get the sum of all the values.

 Do we have a match?

 If so, print the primes.

 And we are done, unless we want the program to print all the matches.

Running it:

```\$ ./prime-partition 18 2 5, 13 \$ ./prime-partition 19 3 3, 5, 11 ```

Use the «-a» command line option to get all the answers, and not just the first one («first» as in the first chosen by the program):

```\$ ./prime-partition -a 18 2 5, 13 7, 11 ```

Looking good.

We can try with a prime, which cannot be partitioned:

```\$ ./prime-partition 19 4 \$ ./prime-partition -a 19 4 ```

Unless you consider a partition of 1 to be a partition:

```\$ ./prime-partition 19 1 19 ```

## Challenge #172.2: Five-number Summary

You are given an array of integers.

Write a script to compute the `five-number summary` of the given set of integers.

File: five-number-summary ```#! /usr/bin/env raku unit sub MAIN (*@integers where all(@integers) ~~ Int, :v(\$verbose)); #  my @sorted = @integers>>.Int.sort; #  say ": Sorted: @sorted[]" if \$verbose; my @fns = #  ( @sorted, #  lower_quartile(@sorted), #  median(@sorted), #  upper_quartile(@sorted), #  @sorted[*-1] #  ); sub median (@values) # [5a] { my \$count = @values.elems; return @values[\$count/2] if \$count % 2; # [5b] return (@values[\$count/2 -0.5] + @values[\$count/2 +0.5]) / 2; # [5c] } sub lower_quartile (@values) # [6a] { my \$count = @values.elems; return median(@values[0 .. \$count/2 -0.5]); } sub upper_quartile (@values) # [7a] { my \$count = @values.elems; return median(@values[\$count/2 .. *]); } say @fns.join(", "); #  ```

 A slurpy argument (`*@`) to get all the values in one array, combined with an `all` junction to ensure that they are all integers.

See docs.raku.org/routine/all for more information about the `all` Junction.

 Sort the values, after coercing them to integers (with `>>.Int`) as they are strings (or `IntStr` rather) when received as input from the command line. Sorting numbers as strings is not a good idea:

```> say <1 2 11 12>.sort; # -> (1 2 11 12) > say "1 2 11 12".words.sort; # -> (1 11 12 2) ```

 Collect the 5 values

 The first value is the one with the lowest value in the array, which is the first one - as the array is sorted.

 The third one (yes, I have skipped the second one - as this one should be explained first) is the median. This is the value in the middle of the array. In the case of an odd number of elements, it is the middle value itself [5b]. If we have an even number of elements, we take the average of the two middle values [5c].

 There are many ways of computing the lower quartile, according to en.wikipedia.org/wiki/Quartile. I have chosen the one called «method 2», where we compute the median of the first half of the values. In the case of an odd number of elements, the middle one is included.

 The upper quartile is the identical to [6a], but we use the second half of the values. The middle value is included here as well, if we have an odd number of elements.

 The fifth value is the one with the highest value in the array, which is the last one - as the array is sorted.

 Print the 5 values.

Running it:

```\$ ./five-number-summary 0 0 1 2 63 61 27 13 0, 0.5, 7.5, 44, 63 ```