CY's Take on PWC#109
If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).
Task 1: Chowla Numbers
# The Weekly Challenge - 109
# Task 1 Chowla Numbers
use strict;
use warnings;
my $F = $ARGV[0] || 20;
my @chowla_seq = (0,0,0);
for my $n (4..$F) {
my $s = 0;
for my $k (2..$n-1) {
$s += $k unless $n % $k;
}
push @chowla_seq, $s;
}
print join ", ", @chowla_seq;
print "\n";
Task 2: Four Squares Puzzle
JavaFX
Perl: Text-Interface
(the graph in task statement) (1) (3) ╔══════════════╗ ╔══════════════╗ ║ ║ ║ ║ ║ a ║ ║ e ║ ║ ║ (2) ║ ║ (4) ║ ┌───╫──────╫───┐ ┌───╫─────────┐ ║ │ ║ ║ │ │ ║ │ ║ │ b ║ ║ d │ │ f ║ │ ║ │ ║ ║ │ │ ║ │ ║ │ ║ ║ │ │ ║ │ ╚══════════╪═══╝ ╚═══╪══════╪═══╝ │ │ c │ │ g │ │ │ │ │ │ │ │ │ └──────────────┘ └─────────────┘
Box->new( Point->new(9,6), Point->new(24,15) ),
Box->new( Point->new(20,10), Point->new(35,19) ),
Box->new( Point->new(31,6), Point->new(46,15) ),
Box->new( Point->new(42,10), Point->new(56,19) ),
);
my @var = (
Point->new(16,8), #a
Point->new(22,12), #b
Point->new(28,16), #c
Point->new(33,12), #d
Point->new(38,8), #e
Point->new(44,12), #f
Point->new(49,16), #g
);
my $N = scalar @boxes;
my @container;
for my $i_b (0..$N-1) {
my $b = $boxes[$i_b];
$container[$i_b] = [];
for my $v_ind (0..$M-1) {
if ( $var[$v_ind]->x < $b->br->x
&& $var[$v_ind]->x > $b->tl->x
&& $var[$v_ind]->y > $b->tl->y
&& $var[$v_ind]->y < $b->br->y) {
push @{$container[$i_b]}, $v_ind;
}
}
}
$ perl ch-2.pl Number of solutions: 8 a b c d e f g 3 7 2 1 5 4 6 4 5 3 1 6 2 7 4 7 1 3 2 6 5 5 6 2 3 1 7 4 6 4 1 5 2 3 7 6 4 5 1 2 7 3 7 2 6 1 3 5 4 7 3 2 5 1 4 6One of the possible solution(s):
**************** ****************
* * * *
* 5 * * 1 *
* * * *
* **************** ***************
* * * * * * * *
* * 6 * * 3 * * 7 * *
* * * * * * * *
* * * * * * * *
**************** **************** *
* 2 * * 4 *
* * * *
* * * *
**************** ***************
Stay alert and healthy! □
Leave a comment