aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaldhar H. Vyas <jaldhar@braincells.com>2021-04-25 18:29:22 -0400
committerJaldhar H. Vyas <jaldhar@braincells.com>2021-04-25 18:29:22 -0400
commit72c62af4a022de098894337b2e5ce3a6419b4653 (patch)
tree3d546a7cea25411385b29cc2335ca7320f612626
parent3531cc83e5609d9a827b954b4bb017bd1124ccbc (diff)
downloadperlweeklychallenge-club-72c62af4a022de098894337b2e5ce3a6419b4653.tar.gz
perlweeklychallenge-club-72c62af4a022de098894337b2e5ce3a6419b4653.tar.bz2
perlweeklychallenge-club-72c62af4a022de098894337b2e5ce3a6419b4653.zip
Challenge 109 by Jaldhar H. Vyas
-rw-r--r--challenge-109/jaldhar-h-vyas/blog.txt1
-rwxr-xr-xchallenge-109/jaldhar-h-vyas/perl/ch-1.pl24
-rwxr-xr-xchallenge-109/jaldhar-h-vyas/perl/ch-2.pl49
-rwxr-xr-xchallenge-109/jaldhar-h-vyas/raku/ch-1.raku15
-rwxr-xr-xchallenge-109/jaldhar-h-vyas/raku/ch-2.raku20
5 files changed, 109 insertions, 0 deletions
diff --git a/challenge-109/jaldhar-h-vyas/blog.txt b/challenge-109/jaldhar-h-vyas/blog.txt
new file mode 100644
index 0000000000..f81f0e890f
--- /dev/null
+++ b/challenge-109/jaldhar-h-vyas/blog.txt
@@ -0,0 +1 @@
+https://www.braincells.com/perl/2021/04/perl_weekly_challenge_week_109.html
diff --git a/challenge-109/jaldhar-h-vyas/perl/ch-1.pl b/challenge-109/jaldhar-h-vyas/perl/ch-1.pl
new file mode 100755
index 0000000000..2e558abfba
--- /dev/null
+++ b/challenge-109/jaldhar-h-vyas/perl/ch-1.pl
@@ -0,0 +1,24 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+
+sub chowla {
+ my ($n) = @_;
+ my $sum = 0;
+
+ for my $i (2 .. $n / 2) {
+ if ($n % $i == 0) {
+ $sum += $i;
+ }
+ }
+
+ return $sum;
+}
+
+my @numbers;
+
+for my $n (1 .. 20) {
+ push @numbers, chowla($n);
+}
+
+say join q{, }, @numbers;
diff --git a/challenge-109/jaldhar-h-vyas/perl/ch-2.pl b/challenge-109/jaldhar-h-vyas/perl/ch-2.pl
new file mode 100755
index 0000000000..3d22af5bed
--- /dev/null
+++ b/challenge-109/jaldhar-h-vyas/perl/ch-2.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+use 5.020;
+use warnings;
+use English qw/ -no_match_vars /;
+
+sub usage {
+ print<<"-USAGE-";
+Usage:
+ $PROGRAM_NAME [<n> ...]
+
+ [<n> ...] 7 integers
+-USAGE-
+ exit 0;
+}
+
+sub permute (&@) {
+ my $code = shift;
+ my @idx = 0..$#_;
+ while ( $code->(@_[@idx]) ) {
+ my $p = $#idx;
+ --$p while $idx[$p-1] > $idx[$p];
+ my $q = $p or return;
+ push @idx, reverse splice @idx, $p;
+ ++$q while $idx[$p-1] > $idx[$q];
+ @idx[$p-1,$q]=@idx[$q,$p-1];
+ }
+}
+
+if (scalar @ARGV != 7) {
+ usage();
+}
+
+my @labels = 'a' .. 'g';
+my @permutations;
+permute { push @permutations, \@_; } @ARGV;
+
+for my $permutation (@permutations) {
+ my $box1 = $permutation->[0] + $permutation->[1];
+ my $box2 = $permutation->[1] + $permutation->[2] + $permutation->[3];
+ my $box3 = $permutation->[3] + $permutation->[4] + $permutation->[5];
+ my $box4 = $permutation->[5] + $permutation->[6];
+
+ if ($box1 == $box2 && $box2 == $box3 && $box3 == $box4) {
+ for my $i (0 .. scalar @{$permutation} - 1) {
+ say $labels[$i], ' = ', $permutation->[$i];
+ }
+ print "\n"
+ }
+}
diff --git a/challenge-109/jaldhar-h-vyas/raku/ch-1.raku b/challenge-109/jaldhar-h-vyas/raku/ch-1.raku
new file mode 100755
index 0000000000..650296611c
--- /dev/null
+++ b/challenge-109/jaldhar-h-vyas/raku/ch-1.raku
@@ -0,0 +1,15 @@
+#!/usr/bin/raku
+
+sub chowla(Int $n) {
+ return [+] (1 ^.. $n div 2).grep({ $n %% $_; });
+}
+
+sub MAIN() {
+ my @numbers;
+
+ for 1 .. 20 -> $n {
+ @numbers.push(chowla($n));
+ }
+
+ @numbers.join(q{, }).say;
+} \ No newline at end of file
diff --git a/challenge-109/jaldhar-h-vyas/raku/ch-2.raku b/challenge-109/jaldhar-h-vyas/raku/ch-2.raku
new file mode 100755
index 0000000000..d6558dd07f
--- /dev/null
+++ b/challenge-109/jaldhar-h-vyas/raku/ch-2.raku
@@ -0,0 +1,20 @@
+#!/usr/bin/raku
+
+sub MAIN(
+ *@n where { @n.elems == 7 } #= 7 integers
+) {
+ my @labels = 'a' .. 'g';
+ for @n.permutations -> @permutation {
+ my $box1 = @permutation[0] + @permutation[1];
+ my $box2 = @permutation[1] + @permutation[2] + @permutation[3];
+ my $box3 = @permutation[3] + @permutation[4] + @permutation[5];
+ my $box4 = @permutation[5] + @permutation[6];
+
+ if $box1 == $box2 == $box3 == $box4 {
+ for 0 ..^ @permutation.elems -> $i {
+ say @labels[$i], ' = ', @permutation[$i];
+ }
+ print "\n"
+ }
+ }
+} \ No newline at end of file