aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-173/bob-lied/README4
-rw-r--r--challenge-173/bob-lied/perl/ch-1.pl56
-rw-r--r--challenge-173/bob-lied/perl/ch-2.pl38
3 files changed, 96 insertions, 2 deletions
diff --git a/challenge-173/bob-lied/README b/challenge-173/bob-lied/README
index c231e3a589..8e35bb24aa 100644
--- a/challenge-173/bob-lied/README
+++ b/challenge-173/bob-lied/README
@@ -1,3 +1,3 @@
-Solutions to weekly challenge 138 by Bob Lied
+Solutions to weekly challenge 173 by Bob Lied
-https://perlweeklychallenge.org/blog/perl-weekly-challenge-138/
+https://perlweeklychallenge.org/blog/perl-weekly-challenge-173/
diff --git a/challenge-173/bob-lied/perl/ch-1.pl b/challenge-173/bob-lied/perl/ch-1.pl
new file mode 100644
index 0000000000..940733d29c
--- /dev/null
+++ b/challenge-173/bob-lied/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-1.pl Perl Weekly Challenge 173 Task 1 Esthetic Number
+#=============================================================================
+# Copyright (c) 2023, Bob Lied
+#=============================================================================
+# You are given a positive integer, $n.
+# Write a script to find out if the given number is Esthetic Number.
+# An esthetic number is a positive integer where every adjacent digit
+# differs from its neighbour by 1. For example,
+# 5456 is an esthetic number as |5 - 4| = |4 - 5| = |5 - 6| = 1
+# 120 is not an esthetic numner as |1 - 2| != |2 - 0| != 1
+#=============================================================================
+
+use v5.38;
+
+use builtin qw/true false/; no warnings "experimental::builtin";
+
+use Getopt::Long;
+my $Verbose = 0;
+my $DoTest = 0;
+
+GetOptions("test" => \$DoTest, "verbose" => \$Verbose);
+exit(!runTest()) if $DoTest;
+
+for my $N ( @ARGV )
+{
+ say "$N ", ( isEsthetic($N) ? "IS " : "is not" ), " esthetic";
+}
+
+sub isEsthetic($n)
+{
+ my $isEth = true;
+
+ my @d = split(//, $n);
+ my $first = shift @d;
+ while ( defined (my $next = shift @d) && $isEth )
+ {
+ $isEth &&= ( ($first - $next == 1) || ( $next - $first == 1 ) );
+ $first = $next;
+ }
+ return $isEth;
+}
+
+sub runTest
+{
+ use Test2::V0;
+ no warnings "experimental::builtin";
+
+ is( isEsthetic(5456), true, "Example 1");
+ is( isEsthetic( 120), false, "Example 2");
+
+ done_testing;
+}
+
diff --git a/challenge-173/bob-lied/perl/ch-2.pl b/challenge-173/bob-lied/perl/ch-2.pl
new file mode 100644
index 0000000000..5c3a4da66f
--- /dev/null
+++ b/challenge-173/bob-lied/perl/ch-2.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
+#=============================================================================
+# ch-2.pl Perl Weekly Challenge 173 Task 2 Sylvester's Sequence
+#=============================================================================
+# Copyright (c) 2023, Bob Lied
+#=============================================================================
+# Write a script to generate first 10 members of Sylvester's sequence.
+# For more informations, please refer to the wikipedia page.
+# https://en.wikipedia.org/wiki/Sylvester%27s_sequence
+# In number theory, Sylvester's sequence is an integer sequence in which
+# each term is the product of the previous terms, plus one.
+#
+# Output
+# 2
+# 3
+# 7
+# 43
+# 1807
+# 3263443
+# 10650056950807
+# 113423713055421844361000443
+# 12864938683278671740537145998360961546653259485195807
+# 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443
+#=============================================================================
+
+use v5.36;
+
+my $max = shift @ARGV // 10;
+
+use bigint;
+my $s = 2;
+say $s;
+foreach (1 .. ($max-1) )
+{
+ $s = $s * ($s - 1) + 1;
+ say $s;
+}