aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-246/perlboy1967/perl/ch1.pl31
-rwxr-xr-xchallenge-246/perlboy1967/perl/ch2.pl59
2 files changed, 90 insertions, 0 deletions
diff --git a/challenge-246/perlboy1967/perl/ch1.pl b/challenge-246/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..c1fdf6ca2b
--- /dev/null
+++ b/challenge-246/perlboy1967/perl/ch1.pl
@@ -0,0 +1,31 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 246
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-246
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: 6 out of 49
+Submitted by: Andreas Voegele
+
+6 out of 49 is a German lottery.
+
+Write a script that outputs six unique random integers from the range 1 to 49.
+
+=cut
+
+use v5.32;
+use common::sense;
+
+use Test2::V0 -srand => 246;
+
+sub germanLottery () {
+ my @balls = (1..49);
+ sort { $a <=> $b } map { splice(@balls,rand $#balls + 1,1) } 1..6;
+}
+
+is([germanLottery],[4,19,20,34,38,46]);
+
+done_testing;
diff --git a/challenge-246/perlboy1967/perl/ch2.pl b/challenge-246/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..9d2000f6a6
--- /dev/null
+++ b/challenge-246/perlboy1967/perl/ch2.pl
@@ -0,0 +1,59 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 246
+- https://theweeklychallenge.org/blog/perl-weekly-challenge-246
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Linear Recurrence of Second Order
+Submitted by: Jorg Sommrey
+
+You are given an array @a of five integers.
+
+Write a script to decide whether the given integers form a linear recurrence
+of second order with integer factors.
+
+A linear recurrence of second order has the form
+
+a[n] = p * a[n-2] + q * a[n-1] with n > 1
+
+where p and q must be integers.
+
+=cut
+
+use v5.32;
+use common::sense;
+use feature qw(signatures);
+
+use Test2::V0;
+
+use List::Util qw(sum0);
+use List::MoreUtils qw(minmax);
+
+sub _chk :prototype($$$\@) ($n,$p,$q,$arL) {
+ $arL->[$n] == $p * $arL->[$n-2] + $q * $arL->[$n-1];
+}
+
+sub linearRecurrenceOfSecondOrder (@) {
+ my @l = @_;
+ my @i = (2..4);
+ my ($min,$max) = minmax(@_);
+
+ for my $p ($min..$max) {
+ for my $q ($min..$max) {
+ return [ $p, $q] if (sum0(map { _chk($_, $p, $q,@l) } @i) == 3);
+ }
+ }
+
+ return [];
+}
+
+
+is(linearRecurrenceOfSecondOrder(1,1,2,3,5),[1,1]);
+is(linearRecurrenceOfSecondOrder(4,2,4,5,7),[]);
+is(linearRecurrenceOfSecondOrder(4,1,2,-3,8),[1,-2]);
+
+done_testing;
+