Perl Weekly Challenge 368.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 368.
Task 1: Make it Bigger
Submitted by: Mohammad Sajid Anwar
You are given a given a string number and a character digit.
Write a script to remove exactly one occurrence of the given
character digit from the given string number, resulting the
decimal form is maximised.
Example 1
Input: $str = "15456", $char = "5"
Output: "1546"
Removing the second "5" is better because the digit
following it (6) is greater than 5. In the first case, 5 was
followed by 4 (a decrease), which makes the resulting number
smaller.  Example 2 Input: $str = "7332", $char = "3"
Output: "732"  Example 3 Input: $str = "2231", $char = "2"
Output: "231"
Removing either "2" results in the same string here. By
removing a "2", we allow the "3" to move up into a higher
decimal place.  Example 4 Input: $str = "543251", $char =
"5" Output: "54321"
If we remove the first "5", the number starts with 4. If we
remove the second "5", the number still starts
with 5. Keeping the largest possible digit in the highest
place value is almost always the priority.  Example 5
Input: $str = "1921", $char = "1" Output: "921" 
Scanning $str from left to right, I should remove the
first instance of $char followed by a larger digit. If
there isn’t any, I should remove the last instance of
$char. If there is no $char, I should simply leave the
string alone. The result fits a dense 2 liner.
perl -E '
for my($s,$c)(@ARGV){@x=split"",$s;splice @x,$i,1 if defined($i=f($c, @x));say "$s, $c -> ",
@x;}sub f($c,@x){my $i;for(0..@x-1){if($x[$_]==$c){$i=$_;return $_ if($x[$_+1]//$c)>=$x[$_];}}$i}
' 15456 5 7332 3 2231 2 543251 5 1921 1
Results:
15456, 5 -> 1546
7332, 3 -> 732
2231, 2 -> 231
543251, 5 -> 54321
1921, 1 -> 921
The full code is:
1 # Perl weekly challenge 368
2 # Task 1: Make it Bigger
3 #
4 # See https://wlmb.github.io/2026/04/06/PWC368/#task-1-make-it-bigger
5 use v5.36;
6 use feature qw(try);
7 die <<~"FIN" unless @ARGV and @ARGV%2==0;
8 Usage: $0 N0 D0 N1 D1...
9 to remove character Dn from number Nn yielding the maximum result.
10 FIN
11 for my($number, $digit)(@ARGV){
12 try {
13 die "Expected a string of digits; $number" unless $number=~/^\d+$/;
14 die "Expected a single digit; $digit" unless $digit=~/^\d$/;
15 my @digits = split "", $number;
16 my $index;
17 splice @digits, $index, 1 if defined($index=which($digit, @digits));
18 my $result=join "", @digits;
19 say "number=$number, digit=$digit -> $result";
20 }
21 catch($e){warn $e;}
22 }
23
24 sub which($digit, @digits){ # Find which digit to remove
25 my $last_found;
26 for(0..@digits - 1){
27 if($digits[$_] == $digit){
28 $last_found = $_;
29 return $_ if($digits[$_+1]//$digit) >= $digits[$_];
30 }
31 }
32 $last_found;
33 }
Example:
./ch-1.pl 15456 5 7332 3 2231 2 543251 5 1921 1
Results:
number=15456, digit=5 -> 1546
number=7332, digit=3 -> 732
number=2231, digit=2 -> 231
number=543251, digit=5 -> 54321
number=1921, digit=1 -> 921
Task 2: Big and Little Omega
Submitted by: Roger Bell_West
You are given a positive integer $number and a mode flag
$mode. If the mode flag is zero, calculate little omega (the
count of all distinct prime factors of that number). If it
is one, calculate big omega (the count of all prime factors
including duplicates).
Example 1
Input: $number = 100061
$mode = 0
Output: 3

Prime factors are 13, 43, 179. Count is 3.
Example 2
Input: $number = 971088
$mode = 0
Output: 3

Prime factors are 2, 2, 2, 2, 3, 20231. Count of distinct
numbers is 3.
Example 3
Input: $number = 63640
$mode = 1
Output: 6

Prime factors are 2, 2, 2, 5, 37, 43. Count including
duplicates is 6.
Example 4
Input: $number = 988841
$mode = 1
Output: 2

Example 5
Input: $number = 211529
$mode = 0
Output: 2
I use the module Math::Prime::Util which has the functions
factor and factor_exp which return big and little omega respectively
when called in scalar context, yielding a simple one-liner.
perl -MMath::Prime::Util=factor,factor_exp -E '
@f=(\&factor_exp,\&factor);for my($n,$m)(@ARGV){say "$n, $m -> ", 0+$f[$m]->($n)}
' 100061 0 971088 0 63640 1 988841 1 211529 0
Results:
100061, 0 -> 3
971088, 0 -> 3
63640, 1 -> 6
988841, 1 -> 2
211529, 0 -> 2
The full code is:
1 # Perl weekly challenge 368
2 # Task 2: Big and Little Omega
3 #
4 # See https://wlmb.github.io/2026/04/06/PWC368/#task-2-big-and-little-omega
5 use v5.36;
6 use feature qw(try);
7 use Math::Prime::Util qw(factor factor_exp);
8 die <<~"FIN" unless @ARGV && @ARGV%2 == 0;
9 Usage: $0 N0 M0 N1 M1...
10 to count prime factors of the number Nn according to the mode Mm.
11 If Mn is 0 count distinct factors. If Mn is 1, count all factors
12 FIN
13 my @function = (\&factor_exp, \&factor);
14 for my($number, $mode)(@ARGV){
15 try {
16 die "Invalid number: $number" unless $number =~ /^\d+$/;
17 die "Invalid mode: $mode" unless $mode==0 || $mode==1;
18 my $result = $function[$mode]->($number);
19 say "number=$number, mode=$mode -> ", $result;
20 }
21 catch($e){ warn $e; }
22 }
23
Example:
./ch-2.pl 100061 0 971088 0 63640 1 988841 1 211529 0
Results:
number=100061, mode=0 -> 3
number=971088, mode=0 -> 3
number=63640, mode=1 -> 6
number=988841, mode=1 -> 2
number=211529, mode=0 -> 2
/;
Written on April 6, 2026