diff options
94 files changed, 5007 insertions, 2168 deletions
diff --git a/challenge-173/athanasius/perl/ch-1.pl b/challenge-173/athanasius/perl/ch-1.pl new file mode 100644 index 0000000000..ece58cccaa --- /dev/null +++ b/challenge-173/athanasius/perl/ch-1.pl @@ -0,0 +1,129 @@ +#!perl + +############################################################################### +=comment + +Perl Weekly Challenge 173 +========================= + +TASK #1 +------- +*Esthetic Number* + +Submitted by: Mohammad S Anwar + +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 number as |1 - 2| != |2 - 0| != 1 + +=cut +############################################################################### + +#--------------------------------------# +# Copyright © 2022 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=comment + +Note on Leading Zeros +--------------------- +$n might be input as "0345434", which would fail the esthetic test because +|0 - 3| != 1. But since $n is specified as a positive integer, I have chosen to +interpret "0345434" as 345434, which passes the test. + +Algorithm +--------- +This is straightforward: test each digit from first to second-last against its +successor: if the absolute value of the difference between them is not 1, $n is +not esthetic; if all digits pass this test, $n is esthetic. + +Note that "differs from its neighbour by 1" is a commutative operation, so it +is only necessary to test the digits in one direction. + +=cut +#============================================================================== + +use strict; +use warnings; +use Const::Fast; +use Regexp::Common qw( number ); + +const my $USAGE => +"Usage: + perl $0 <n> + + <n> A positive integer\n"; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + $| = 1; + print "\nChallenge 173, Task #1: Esthetic Number (Perl)\n\n"; +} + +#============================================================================== +MAIN: +#============================================================================== +{ + my $n = parse_command_line(); + my $esthetic = is_esthetic( $n ); + + printf "%d is%s an esthetic number\n", $n, $esthetic ? '' : ' not'; +} + +#------------------------------------------------------------------------------ +sub is_esthetic +#------------------------------------------------------------------------------ +{ + my ($n) = @_; + my @digits = split //, $n; + + for my $i (0 .. $#digits - 1) + { + return 0 unless abs( $digits[ $i ] - $digits[ $i + 1 ] ) == 1; + } + + return 1; +} + +#------------------------------------------------------------------------------ +sub parse_command_line +#------------------------------------------------------------------------------ +{ + my $args = scalar @ARGV; + $args == 1 or error( "Expected 1 command line argument, found $args" ); + + my $n = $ARGV[ 0 ]; + + $n =~ / ^ $RE{num}{int} $ /x + or error( qq[Argument "$n" is not a valid integer] ); + + $n > 0 or error( qq[Argument "$n" is not positive] ); + + $n += 0; # Remove any initial zeros + + return $n; +} + +#------------------------------------------------------------------------------ +sub error +#------------------------------------------------------------------------------ +{ + my ($message) = @_; + + die "ERROR: $message\n$USAGE"; +} + +############################################################################### diff --git a/challenge-173/athanasius/perl/ch-2.pl b/challenge-173/athanasius/perl/ch-2.pl new file mode 100644 index 0000000000..c8419c9352 --- /dev/null +++ b/challenge-173/athanasius/perl/ch-2.pl @@ -0,0 +1,129 @@ +#!perl + +############################################################################### +=comment + +Perl Weekly Challenge 173 +========================= + +TASK #2 +------- +*Sylvester’s sequence* + +Submitted by: Mohammad S Anwar + +Write a script to generate first 10 members of Sylvester's sequence. For more +informations, please refer to the [ https://en.wikipedia.org/wiki/Sylvester +%27s_sequence |wikipedia page]. + +Output + + 2 + 3 + 7 + 43 + 1807 + 3263443 + 10650056950807 + 113423713055421844361000443 + 12864938683278671740537145998360961546653259485195807 + 165506647324519964198468195444439180017513152706377497841851388766535868639 + 572406808911988131737645185443 + +=cut +############################################################################### + +#--------------------------------------# +# Copyright © 2022 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=comment + +Interface +--------- +Since numbers in Sylvester's sequence quickly become very large, they may be +easier to read with their digits separated into groups of 3. To do this, set +the constant $SEP to the value of the separator required (e.g., a comma or a +space). + +Algorithm +--------- +Large numbers are accommodated via the "use bigint;" pragma. + +Successive elements of Sylvester's sequence are calculated using the recurrence +relation: + + a(n+1) = a(n)² - a(n) + 1 + +from [1]. This is re-arranged to the equivalent: + + a(n+1) = a(n) × (a(n) - 1) + 1. + +References +---------- +[1] "A000058 Sylvester's sequence: a(n+1) = a(n)^2 - a(n) + 1, with a(0) = 2.", + OEIS, https://oeis.org/A000058 +[2] "Sylvester's sequence", Wikipedia, + https://en.wikipedia.org/wiki/Sylvester%27s_sequence + +=cut +#============================================================================== + +use strict; +use warnings; +use bigint; +use Const::Fast; + +const my $SEP => ''; +const my $TARGET => 10; +const my $USAGE => "Usage:\n perl $0\n"; +const my $WRAP => 72; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + $| = 1; + print "\nChallenge 173, Task #2: Sylvester's sequence (Perl)\n\n"; +} + +#============================================================================== +MAIN: +#============================================================================== +{ + my $args = scalar @ARGV; + $args == 0 or die 'ERROR: Expected 0 command line arguments, found ' . + "$args\n$USAGE"; + + print "The first $TARGET numbers of Sylvester's sequence:\n"; + + my $sylvester = 2; + + printf "%2d: %d\n", 1, $sylvester; + + for my $i (2 .. $TARGET) + { + $sylvester *= $sylvester - 1; + ++$sylvester; + + my $string = $SEP ? add_separators( $sylvester, $SEP ) : $sylvester; + $string = substr( $string, 0, $WRAP ) . "\n " . + substr( $string, $WRAP ) if length $string > $WRAP; + + printf "%2d: %s\n", $i, $string; + } +} + +#------------------------------------------------------------------------------- +sub add_separators +#------------------------------------------------------------------------------- +{ + my ($num, $sep) = @_; + + # Regex from perlfaq5: "How can I output my numbers with commas added?" + + return $num =~ s/(^\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1$sep/gr; +} + +############################################################################### diff --git a/challenge-173/athanasius/raku/ch-1.raku b/challenge-173/athanasius/raku/ch-1.raku new file mode 100644 index 0000000000..5275f1e309 --- /dev/null +++ b/challenge-173/athanasius/raku/ch-1.raku @@ -0,0 +1,103 @@ +use v6d; + +############################################################################### +=begin comment + +Perl Weekly Challenge 173 +========================= + +TASK #1 +------- +*Esthetic Number* + +Submitted by: Mohammad S Anwar + +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 number as |1 - 2| != |2 - 0| != 1 + +=end comment +############################################################################### + +#--------------------------------------# +# Copyright © 2022 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=begin comment + +Note on Leading Zeros +--------------------- +$n might be input as "0345434", which would fail the esthetic test because +|0 - 3| != 1. But since $n is specified as a positive integer, I have chosen to +interpret "0345434" as 345434, which passes the test. + +Algorithm +--------- +This is straightforward: test each digit from first to second-last against its +successor: if the absolute value of the difference between them is not 1, $n is +not esthetic; if all digits pass this test, $n is esthetic. + +Note that "differs from its neighbour by 1" is a commutative operation, so it +is only necessary to test the digits in one direction. + +=end comment +#============================================================================= |
