From 28b05ff116532b4cb83476f578309646ba0105a3 Mon Sep 17 00:00:00 2001 From: habere-et-dispertire Date: Thu, 14 Jul 2022 12:49:39 +0100 Subject: [Raku] Esthetic Number --- challenge-173/habere-et-dispetire/raku/ch-1.raku | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 challenge-173/habere-et-dispetire/raku/ch-1.raku diff --git a/challenge-173/habere-et-dispetire/raku/ch-1.raku b/challenge-173/habere-et-dispetire/raku/ch-1.raku new file mode 100644 index 0000000000..d712a756ce --- /dev/null +++ b/challenge-173/habere-et-dispetire/raku/ch-1.raku @@ -0,0 +1,15 @@ +#! /usr/bin/env raku + +# Esthetic Number +# https://oeis.org/A033075 + +sub is-esthetic ( $n ) { + + $n.comb + .rotor( 2 => -1 ) + .flat + .map( (* - *).abs == 1 ) + .all + .so + +} -- cgit From 6b35010281c612bb4de18c6839c1cfb86f9ca64d Mon Sep 17 00:00:00 2001 From: habere-et-dispertire Date: Thu, 14 Jul 2022 12:51:09 +0100 Subject: [raku] Sylvester’s sequence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- challenge-173/habere-et-dispetire/raku/ch-2.raku | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 challenge-173/habere-et-dispetire/raku/ch-2.raku diff --git a/challenge-173/habere-et-dispetire/raku/ch-2.raku b/challenge-173/habere-et-dispetire/raku/ch-2.raku new file mode 100644 index 0000000000..ef19b95e3f --- /dev/null +++ b/challenge-173/habere-et-dispetire/raku/ch-2.raku @@ -0,0 +1,9 @@ +#! /usr/bin/env raku + +# Sylvester’s sequence +# https://oeis.org/A000058 + +multi sylvester ( 0 ) { 2 } +multi sylvester ( $n ) { sylvester( $n-1 )² - sylvester( $n-1 ) + 1 } + +say .&sylvester for ^10 -- cgit From 5a03145cfc5e756dfa8b109686416b3627cef722 Mon Sep 17 00:00:00 2001 From: PerlMonk-Athanasius Date: Thu, 14 Jul 2022 22:03:02 +1000 Subject: Perl & Raku solutions to Tasks 1 & 2 for Week 173 --- challenge-173/athanasius/perl/ch-1.pl | 129 +++++++++++++++++++++++++++++++ challenge-173/athanasius/perl/ch-2.pl | 129 +++++++++++++++++++++++++++++++ challenge-173/athanasius/raku/ch-1.raku | 103 +++++++++++++++++++++++++ challenge-173/athanasius/raku/ch-2.raku | 132 ++++++++++++++++++++++++++++++++ 4 files changed, 493 insertions(+) create mode 100644 challenge-173/athanasius/perl/ch-1.pl create mode 100644 challenge-173/athanasius/perl/ch-2.pl create mode 100644 challenge-173/athanasius/raku/ch-1.raku create mode 100644 challenge-173/athanasius/raku/ch-2.raku 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 + + 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 +#============================================================================== + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + "\nChallenge 173, Task #1: Esthetic Number (Raku)\n".put; +} + +#============================================================================== +sub MAIN +( + UInt:D $n where * > 0 #= A positive integer +) +#============================================================================== +{ + my UInt $m = $n + 0; # Remove any initial zeros + + my Bool $esthetic = is-esthetic( $m ); + + "%d is%s an esthetic number\n".printf: $m, $esthetic ?? '' !! ' not'; +} + +#------------------------------------------------------------------------------ +sub is-esthetic( UInt:D $n where * > 0 --> Bool:D ) +#------------------------------------------------------------------------------ +{ + my UInt @digits = $n.split( '', :skip-empty ).map: { .Int }; + + for 0 .. @digits.end - 1 -> UInt $i + { + return False unless (@digits[ $i ] - @digits[ $i + 1 ]).abs == 1; + } + + return True; +} + +#------------------------------------------------------------------------------ +sub USAGE() +#------------------------------------------------------------------------------ +{ + my Str $usage = $*USAGE; + + $usage ~~ s/ ($*PROGRAM-NAME) /raku $0/; + + $usage.put; +} + +############################################################################### diff --git a/challenge-173/athanasius/raku/ch-2.raku b/challenge-173/athanasius/raku/ch-2.raku new file mode 100644 index 0000000000..43acfe442f --- /dev/null +++ b/challenge-173/athanasius/raku/ch-2.raku @@ -0,0 +1,132 @@ +use v6d; + +############################################################################### +=begin 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 + +=end comment +############################################################################### + +#--------------------------------------# +# Copyright © 2022 PerlMonk Athanasius # +#--------------------------------------# + +#============================================================================== +=begin 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 +--------- +The large numbers are automatically accommodated by using the Raku type Int, +which "offers arbitrary-size integer numbers". [2] + +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] "Numerics", Raku Documentation, https://docs.raku.org/language/numerics +[3] "Sylvester's sequence", Wikipedia, + https://en.wikipedia.org/wiki/Sylvester%27s_sequence + +=end comment +#============================================================================== + +my Str constant $SEP = ''; +my UInt constant $TARGET = 10; +my UInt constant $WRAP = 72; + +#------------------------------------------------------------------------------ +BEGIN +#------------------------------------------------------------------------------ +{ + "\nChallenge 173, Task #2: Sylvester's sequence (Raku)\n".put; +} + +#============================================================================== +sub MAIN() +#============================================================================== +{ + "The first $TARGET numbers of Sylvester's sequence:".put; + + my UInt $sylvester = 2; + + "%2d: %d\n".printf: 1, $sylvester; + + for 2 .. $TARGET -> UInt $i + { + $sylvester *= $sylvester - 1; + ++$sylvester; + + my Str $string = $SEP ?? add-commas( $sylvester, $SEP ) + !! $sylvester.Str; + $string = $string.substr( 0, $WRAP ) ~ "\n " ~ + $string.substr( $WRAP ) if $string.chars > $WRAP; + + "%2d: %s\n".printf: $i, $string; + } +} + +#------------------------------------------------------------------------------- +# Adapted from commatize() at +# http://www.rosettacode.org/wiki/Commatizing_numbers#Raku +# +sub add-commas( Int:D $number, Str:D $sep --> Str:D ) +#------------------------------------------------------------------------------- +{ + $number.subst: / <[ 1 .. 9 ]> <[ 0 .. 9 ]>* /, + *.flip.comb( / <{ ".**1..3" }> / ).join( $sep ).flip; +} + +#------------------------------------------------------------------------------ +sub USAGE() +#------------------------------------------------------------------------------ +{ + my Str $usage = $*USAGE; + + $usage ~~ s:g/ ($*PROGRAM-NAME) /raku $0/; + + $usage.put; +} + +############################################################################### -- cgit From af2b2b4c1cf7e337e9c5d2bdf3d7fdfa0570641c Mon Sep 17 00:00:00 2001 From: Simon Green Date: Thu, 14 Jul 2022 23:16:39 +1000 Subject: sgreen solutions to challenge 173 --- challenge-173/sgreen/README.md | 4 ++-- challenge-173/sgreen/blog.txt | 1 + challenge-173/sgreen/perl/ch-1.pl | 24 ++++++++++++++++++++++++ challenge-173/sgreen/perl/ch-2.pl | 22 ++++++++++++++++++++++ challenge-173/sgreen/python/ch-1.py | 21 +++++++++++++++++++++ challenge-173/sgreen/python/ch-2.py | 18 ++++++++++++++++++ 6 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 challenge-173/sgreen/blog.txt create mode 100755 challenge-173/sgreen/perl/ch-1.pl create mode 100755 challenge-173/sgreen/perl/ch-2.pl create mode 100755 challenge-173/sgreen/python/ch-1.py create mode 100755 challenge-173/sgreen/python/ch-2.py diff --git a/challenge-173/sgreen/README.md b/challenge-173/sgreen/README.md index 327abfe81e..c4ff584191 100644 --- a/challenge-173/sgreen/README.md +++ b/challenge-173/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 172 +# The Weekly Challenge 173 -[Blog](https://dev.to/simongreennet/weekly-challenge-172-1h8i) +[Blog](https://dev.to/simongreennet/weekly-challenge-173-1kli) diff --git a/challenge-173/sgreen/blog.txt b/challenge-173/sgreen/blog.txt new file mode 100644 index 0000000000..3f796988da --- /dev/null +++ b/challenge-173/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-173-1kli \ No newline at end of file diff --git a/challenge-173/sgreen/perl/ch-1.pl b/challenge-173/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..0e9adf26d8 --- /dev/null +++ b/challenge-173/sgreen/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ($n) { + # The first character is 0 .. len(n)-2 + foreach my $i ( 0 .. length($n) - 2 ) { + # Calculate the difference between that number and the next + my $diff = abs( substr( $n, $i, 1 ) - substr( $n, $i + 1, 1 ) ); + if ( $diff != 1 ) { + # This is not a esthetic number + say '0'; + return; + } + } + + # This is a esthetic number + say '1'; +} + +main(@ARGV); \ No newline at end of file diff --git a/challenge-173/sgreen/perl/ch-2.pl b/challenge-173/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..84499570bb --- /dev/null +++ b/challenge-173/sgreen/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use List::Util 'product'; +use Math::BigInt; + +sub main { + # Set S0 of 2 + my @solutions = ( Math::BigInt->new(2) ); + + while ( @solutions < 10 ) { + # The next value is the product of the current array plus one + push @solutions, product(@solutions) + 1; + } + + # Print the solution + say foreach @solutions; +} + +main(); \ No newline at end of file diff --git a/challenge-173/sgreen/python/ch-1.py b/challenge-173/sgreen/python/ch-1.py new file mode 100755 index 0000000000..ab78b2aa42 --- /dev/null +++ b/challenge-173/sgreen/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +import sys + + +def main(n): + # The first character is 0 .. len(n)-2 + for i in range(len(n)-1): + # Calculate the difference between that number and the next + diff = abs(int(n[i]) - int(n[i+1])) + if diff != 1: + # This is not a esthetic number + print('0') + return + + # This is a esthetic number + print('1') + + +if __name__ == '__main__': + main(sys.argv[1]) diff --git a/challenge-173/sgreen/python/ch-2.py b/challenge-173/sgreen/python/ch-2.py new file mode 100755 index 0000000000..fb5a135067 --- /dev/null +++ b/challenge-173/sgreen/python/ch-2.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python + +import math + + +def main(): + # Set S0 of 2 + solutions = [2] + while len(solutions) < 10: + # The next value is the product of the current array plus one + solutions.append(math.prod(solutions)+1) + + # Print the solution + print(*solutions, sep='\n') + + +if __name__ == '__main__': + main() -- cgit From 76a1cabdcb450ae50377df0ce0b4fa5998667647 Mon Sep 17 00:00:00 2001 From: deadmarshal Date: Fri, 15 Jul 2022 15:25:22 +0430 Subject: Challenge173 --- challenge-173/deadmarshal/ada/Ch2.gpr | 10 ++++ challenge-173/deadmarshal/ada/ch1.adb | 31 +++++++++++++ challenge-173/deadmarshal/ada/ch2.adb | 30 ++++++++++++ challenge-173/deadmarshal/c/Makefile | 12 +++++ challenge-173/deadmarshal/c/ch-1.c | 34 ++++++++++++++ challenge-173/deadmarshal/c/ch-2.c | 53 ++++++++++++++++++++++ challenge-173/deadmarshal/d/ch1.d | 32 +++++++++++++ challenge-173/deadmarshal/d/ch2.d | 14 ++++++ challenge-173/deadmarshal/lua/ch-1.lua | 23 ++++++++++ challenge-173/deadmarshal/lua/ch-2.lua | 24 ++++++++++ challenge-173/deadmarshal/modula-3/ch1/src/Ch1.m3 | 40 ++++++++++++++++ .../deadmarshal/modula-3/ch1/src/m3makefile | 5 ++ challenge-173/deadmarshal/modula-3/ch2/src/Ch2.m3 | 29 ++++++++++++ .../deadmarshal/modula-3/ch2/src/m3makefile | 5 ++ challenge-173/deadmarshal/perl/ch-1.pl | 31 +++++++++++++ challenge-173/deadmarshal/perl/ch-2.pl | 15 ++++++ 16 files changed, 388 insertions(+) create mode 100644 challenge-173/deadmarshal/ada/Ch2.gpr create mode 100644 challenge-173/deadmarshal/ada/ch1.adb create mode 100644 challenge-173/deadmarshal/ada/ch2.adb create mode 100644 challenge-173/deadmarshal/c/Makefile create mode 100644 challenge-173/deadmarshal/c/ch-1.c create mode 100644 challenge-173/deadmarshal/c/ch-2.c create mode 100644 challenge-173/deadmarshal/d/ch1.d create mode 100644 challenge-173/deadmarshal/d/ch2.d create mode 100644 challenge-173/deadmarshal/lua/ch-1.lua create mode 100644 challenge-173/deadmarshal/lua/ch-2.lua create mode 100644 challenge-173/deadmarshal/modula-3/ch1/src/Ch1.m3 create mode 100644 challenge-173/deadmarshal/modula-3/ch1/src/m3makefile create mode 100644 challenge-173/deadmarshal/modula-3/ch2/src/Ch2.m3 create mode 100644 challenge-173/deadmarshal/modula-3/ch2/src/m3makefile create mode 100644 challenge-173/deadmarshal/perl/ch-1.pl create mode 100644 challenge-173/deadmarshal/perl/ch-2.pl diff --git a/challenge-173/deadmarshal/ada/Ch2.gpr b/challenge-173/deadmarshal/ada/Ch2.gpr new file mode 100644 index 0000000000..c5634a7337 --- /dev/null +++ b/challenge-173/deadmarshal/ada/Ch2.gpr @@ -0,0 +1,10 @@ +with "gnatcoll"; +with "gnatcoll_gmp"; + +project Ch2 is + for Languages use ("Ada"); + for Create_Missing_Dirs use "True"; + for Source_Dirs use ("."); + for Object_Dir use "build"; + for Exec_Dir use "."; +end Ch2; diff --git a/challenge-173/deadmarshal/ada/ch1.adb b/challenge-173/deadmarshal/ada/ch1.adb new file mode 100644 index 0000000000..5bcefe1f3e --- /dev/null +++ b/challenge-173/deadmarshal/ada/ch1.adb @@ -0,0 +1,31 @@ +-- Compile with: gnatmake ch1.adb +with Ada.Text_IO;use Ada.Text_IO; + +procedure Ch1 is + function is_esthetic_number(N:in out Integer) return Boolean is + function Uabs(A,B:Integer) return Integer is + begin + if a < b then return b - a; else return a - b; end if; + end Uabs; + + I,J:Integer; + begin + if N = 0 then return False; end if; + I := N mod 10; + N := N / 10; + while(N > 0) loop + J := N mod 10; + if Uabs(I, J) /= 1 then return False; end if; + N := N / 10; + I := J; + end loop; + return True; + end is_esthetic_number; + + N:Integer; +begin + Put("Enter an integeral number: "); + N := Integer'Value(Get_Line); + Put_Line(Boolean'Image(is_esthetic_number(N))); +end Ch1; + diff --git a/challenge-173/deadmarshal/ada/ch2.adb b/challenge-173/deadmarshal/ada/ch2.adb new file mode 100644 index 0000000000..335c694fd8 --- /dev/null +++ b/challenge-173/deadmarshal/ada/ch2.adb @@ -0,0 +1,30 @@ +-- Compile with: gprbuild ch2.adb +with Ada.Text_IO; +with GNATCOLL.GMP;use GNATCOLL.GMP; +with GNATCOLL.GMP.Integers;use GNATCOLL.GMP.Integers; +with GNATCOLL.GMP.Integers.IO;use GNATCOLL.GMP.Integers.IO; + +procedure Ch2 is + type TArray is array (0..9) of Big_Integer; + Arr:TArray; + K:Integer := 2; +begin + Set(Arr(0), 2); + Set(Arr(1), 3); + + for I in 2..Arr'Last loop + Set(Arr(I), 1); + end loop; + + for I in 2..9 loop + for J in 0..K-1 loop + Multiply(Arr(K), Arr(K), Arr(J)); + end loop; + Add(Arr(K), 1); + K := K + 1; + end loop; + + for I in Arr'Range loop + Ada.Text_IO.Put_Line(Image(Arr(I))); + end loop; +end Ch2; diff --git a/challenge-173/deadmarshal/c/Makefile b/challenge-173/deadmarshal/c/Makefile new file mode 100644 index 0000000000..2b74c40769 --- /dev/null +++ b/challenge-173/deadmarshal/c/Makefile @@ -0,0 +1,12 @@ +CFLAGS=-Wall -Wshadow -pedantic --std=c99 +LIBS=$(shell pkg-config --libs gmp) + +all: ch-1 ch-2 + +ch-1: + $(CC) $(CFLAGS) -o ch-1 ch-1.c +ch-2: + $(CC) $(CFLAGS) -o ch-2 ch-2.c $(LIBS) + +clean: + rm -rf ch-1 ch-2 diff --git a/challenge-173/deadmarshal/c/ch-1.c b/challenge-173/deadmarshal/c/ch-1.c new file mode 100644 index 0000000000..ac7954fd95 --- /dev/null +++ b/challenge-173/deadmarshal/c/ch-1.c @@ -0,0 +1,34 @@ +#include + +int uabs(int a, int b) +{ + if(a < b) return b - a; + return a - b; +} + +int is_esthetic_number(int n) +{ + if(n == 0) return 0; + int i = n % 10; + n /= 10; + while(n) + { + int j = n % 10; + if(uabs(i, j) != 1) return 0; + n /= 10; + i = j; + } + return 1; +} + +int main(void) +{ + printf("Is %d an esthetic number? %s\n", + 5456, + is_esthetic_number(5456) ? "true" : "false"); + printf("Is %d an esthetic number? %s\n", + 120, + is_esthetic_number(120) ? "true" : "false"); + + return 0; +} diff --git a/challenge-173/deadmarshal/c/ch-2.c b/challenge-173/deadmarshal/c/ch-2.c new file mode 100644 index 0000000000..a41ef664df --- /dev/null +++ b/challenge-173/deadmarshal/c/ch-2.c @@ -0,0 +1,53 @@ +#include + +// Sylvester’s sequence +#define N 10 + +void array_init(mpz_t *arr, size_t size) +{ + for(int i = 0; i < size; ++i) + mpz_init(arr[i]); +} + +void array_set(mpz_t *arr, size_t size) +{ + mpz_set_ui(arr[0], 2); + mpz_set_ui(arr[1], 3); + for(int i = 2; i < N; ++i) + mpz_set_ui(arr[i], 1); +} + +void array_mul(mpz_t *arr, size_t size) +{ + int k = 2; + for(int i = 2; i < N; ++i) + { + for(int j = 0; j < k; ++j) + mpz_mul(arr[k], arr[k], arr[j]); + mpz_add_ui(arr[k], arr[k], 1); + k++; + } +} + +void array_print(mpz_t *arr, size_t size) +{ + for(int i = 0; i < N; ++i) + gmp_printf("%Zd\n", arr[i]); +} + +void array_free(mpz_t *arr, size_t size) +{ + for(int i = 0; i < N; ++i) + mpz_clear(arr[i]); +} + +int main(void) +{ + mpz_t arr[N]; + array_init(arr, N); + array_set(arr, N); + array_mul(arr, N); + array_print(arr, N); + array_free(arr, N); + return 0; +} diff --git a/challenge-173/deadmarshal/d/ch1.d b/challenge-173/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..7ed32f3762 --- /dev/null +++ b/challenge-173/deadmarshal/d/ch1.d @@ -0,0 +1,32 @@ +import std.stdio; + +int uabs(int a, int b) +{ + if(a < b) return b - a; + return a - b; +} + +bool is_esthetic_number(int n) +{ + if(n == 0) return false; + int i = n % 10; + n /= 10; + while(n) + { + int j = n % 10; + if(uabs(i, j) != 1) return false; + n /= 10; + i = j; + } + return true; +} + +void main() +{ + writefln("Is %d an esthetic number? %s", + 5456, + is_esthetic_number(5456)); + writefln("Is %d an esthetic number? %s", + 120, + is_esthetic_number(120)); +} diff --git a/challenge-173/deadmarshal/d/ch2.d b/challenge-173/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..1a2b0b38d4 --- /dev/null +++ b/challenge-173/deadmarshal/d/ch2.d @@ -0,0 +1,14 @@ +import std.stdio:writeln; +import std.bigint; +import std.algorithm.iteration:reduce; + +void main() +{ + BigInt[] arr = [2.BigInt,3.BigInt]; + do{ + arr ~= reduce!((a, b) => a * b)(1.BigInt,arr) + 1; + }while(arr.length != 10); + + foreach(e; arr) + writeln(e); +} diff --git a/challenge-173/deadmarshal/lua/ch-1.lua b/challenge-173/deadmarshal/lua/ch-1.lua new file mode 100644 index 0000000000..c25a62fb47 --- /dev/null +++ b/challenge-173/deadmarshal/lua/ch-1.lua @@ -0,0 +1,23 @@ +if #arg ~= 1 then + io.stderr:write('No args provided!') + os.exit(1) +end + +local function is_esthetic_number(n) + assert(type(n) == 'number', 'n must be a number!') + local function uabs(a, b) + if a < b then return b - a else return a - b end + end + if n == 0 then return false end + local i = n % 10 + n = n // 10 + while n > 0 do + local j = n % 10 + if uabs(i, j) ~= 1 then return false end + n = n // 10 + i = j + end + return true +end + +print(is_esthetic_number(tonumber(arg[1]))) diff --git a/challenge-173/deadmarshal/lua/ch-2.lua b/challenge-173/deadmarshal/lua/ch-2.lua new file mode 100644 index 0000000000..1cca518763 --- /dev/null +++ b/challenge-173/deadmarshal/lua/ch-2.lua @@ -0,0 +1,24 @@ +-- https://github.com/edubart/lua-bint +-- To install this library: luarocks install bint +local bint = require 'bint'(1024) + +local function product(t, last) + assert(type(t) == 'table' and + type(last) == 'number', + 't, last must be table, number, number respectively!') + local prod = bint(1) + for i=1, last do prod = prod * t[i] end + return prod +end + +local function sylvesters_sequence() + local t = {2,3} + while(#t ~= 10) do + t[#t+1] = bint(product(t, #t) + 1) + end + return t +end + +local t = sylvesters_sequence() +for i=1, #t do print(t[i]) end + diff --git a/challenge-173/deadmarshal/modula-3/ch1/src/Ch1.m3 b/challenge-173/deadmarshal/modula-3/ch1/src/Ch1.m3 new file mode 100644 index 0000000000..e8e1091340 --- /dev/null +++ b/challenge-173/deadmarshal/modula-3/ch1/src/Ch1.m3 @@ -0,0 +1,40 @@ +MODULE Ch1 EXPORTS Main; + +IMPORT IO, Scan, Lex, FloatMode, StableError, Params; +FROM Fmt IMPORT Bool; + +VAR + Input:INTEGER; + +PROCEDURE IsEsthetic(N:INTEGER):BOOLEAN = +PROCEDURE Uabs(READONLY A,B:INTEGER):INTEGER = +BEGIN + IF A < B THEN RETURN B - A END; + RETURN A - B; +END Uabs; +VAR + I,J:INTEGER; +BEGIN + IF N = 0 THEN RETURN FALSE END; + I := N MOD 10; + N := N DIV 10; + WHILE N > 0 DO + J := N MOD 10; + IF Uabs(I,J) # 1 THEN RETURN FALSE END; + N := N DIV 10; + I := J; + END; + RETURN TRUE; +END IsEsthetic; + +BEGIN + IF Params.Count # 2 THEN StableError.Halt("No arg(s) provided!") END; + TRY + Input := Scan.Int(Params.Get(1)); + EXCEPT + | Lex.Error, FloatMode.Trap => + IO.Put("Malformed arg(s) provided!"); + END; + + IO.Put(Bool(IsEsthetic(Input))); +END Ch1. diff --git a/challenge-173/deadmarshal/modula-3/ch1/src/m3makefile b/challenge-173/deadmarshal/modula-3/ch1/src/m3makefile new file mode 100644 index 0000000000..db449d6bcf --- /dev/null +++ b/challenge-173/deadmarshal/modula-3/ch1/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("stable") + +implementation("Ch1") +program("ch1") diff --git a/challenge-173/deadmarshal/modula-3/ch2/src/Ch2.m3 b/challenge-173/deadmarshal/modula-3/ch2/src/Ch2.m3 new file mode 100644 index 0000000000..c2d726c3f8 --- /dev/null +++ b/challenge-173/deadmarshal/modula-3/ch2/src/Ch2.m3 @@ -0,0 +1,29 @@ +MODULE Ch2 EXPORTS Main; + +IMPORT IO; +FROM BigInteger IMPORT T,Add,Mul,FromInteger; +FROM BigIntegerFmtLex IMPORT Fmt; + +PROCEDURE SylvestersSequence() = +VAR + K:INTEGER := 2; + Arr:ARRAY[0..9] OF T; +BEGIN + Arr[0] := FromInteger(2); + Arr[1] := FromInteger(3); + FOR I := 2 TO LAST(Arr) DO Arr[I] := FromInteger(1) END; + FOR I := 2 TO LAST(Arr) DO + FOR J := 0 TO K-1 DO + Arr[K] := Mul(Arr[K], Arr[J]); + END; + Arr[K] := Add(Arr[K], FromInteger(1)); + INC(K); + END; + FOR I := FIRST(Arr) TO LAST(Arr) DO + IO.Put(Fmt(Arr[I]) & "\n"); + END; +END SylvestersSequence; + +BEGIN + SylvestersSequence(); +END Ch2. diff --git a/challenge-173/deadmarshal/modula-3/ch2/src/m3makefile b/challenge-173/deadmarshal/modula-3/ch2/src/m3makefile new file mode 100644 index 0000000000..9503562257 --- /dev/null +++ b/challenge-173/deadmarshal/modula-3/ch2/src/m3makefile @@ -0,0 +1,5 @@ +import("libm3") +import("arithmetic") + +implementation("Ch2") +program("ch2") diff --git a/challenge-173/deadmarshal/perl/ch-1.pl b/challenge-173/deadmarshal/perl/ch-1.pl new file mode 100644 index 0000000000..eb6ea8a9c9 --- /dev/null +++ b/challenge-173/deadmarshal/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +use strict; +use warnings; + +die "No args provided!" unless @ARGV == 1 && $ARGV[0] =~ /\d+/; + +sub is_esthetic_number{ + my ($n) = @_; + use integer; + my $uabs = sub{ + if($_[0] < $_[1]){ + return $_[1] - $_[0]; + } + return $_[0] - $_[1]; + }; + + if($n == 0){return 0} + my $i = $n % 10; + $n /= 10; + while($n){ + my $j = $n % 10; + if($uabs->($i, $j) != 1){ + return 0; + } + $n /= 10; + $i = $j; + } + return 1; +} + +print is_esthetic_number($ARGV[0]) ? "True" : "False", "\n"; diff --git a/challenge-173/deadmarshal/perl/ch-2.pl b/challenge-173/deadmarshal/perl/ch-2.pl new file mode 100644 index 0000000000..12a479c26e --- /dev/null +++ b/challenge-173/deadmarshal/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use List::Util qw(product); +use Math::BigInt lib => 'GMP'; + +sub sylvesters_sequence{ + my @arr = (2,3); + do{ + push @arr, Math::BigInt->new(product @arr[0..@arr-1]) + 1; + }until(@arr == 10); + return @arr; +} + +print "$_\n" foreach(sylvesters_sequence); -- cgit From ee0e99f7095d31efcf03bea08c1edaa27f8ce38c Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Fri, 15 Jul 2022 12:29:25 +0100 Subject: Solutions for challenge #173 --- challenge-173/roger-bell-west/javascript/ch-1.js | 60 +++++++++++++++++++ challenge-173/roger-bell-west/javascript/ch-2.js | 56 ++++++++++++++++++ challenge-173/roger-bell-west/kotlin/ch-1.kt | 60 +++++++++++++++++++ challenge-173/roger-bell-west/kotlin/ch-2.kt | 31 ++++++++++ challenge-173/roger-bell-west/lua/ch-1.lua | 56 ++++++++++++++++++ challenge-173/roger-bell-west/perl/ch-1.pl | 47 +++++++++++++++ challenge-173/roger-bell-west/perl/ch-2.pl | 32 ++++++++++ challenge-173/roger-bell-west/postscript/ch-1.ps | 74 ++++++++++++++++++++++++ challenge-173/roger-bell-west/python/ch-1.py | 38 ++++++++++++ challenge-173/roger-bell-west/python/ch-2.py | 27 +++++++++ challenge-173/roger-bell-west/raku/ch-1.p6 | 46 +++++++++++++++ challenge-173/roger-bell-west/raku/ch-2.p6 | 28 +++++++++ challenge-173/roger-bell-west/ruby/ch-1.rb | 46 +++++++++++++++ challenge-173/roger-bell-west/ruby/ch-2.rb | 30 ++++++++++ challenge-173/roger-bell-west/rust/ch-1.rs | 47 +++++++++++++++ challenge-173/roger-bell-west/rust/ch-2.rs | 28 +++++++++ 16 files changed, 706 insertions(+) create mode 100755 challenge-173/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-173/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-173/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-173/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-173/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-173/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-173/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-173/roger-bell-west/postscript/ch-1.ps create mode 100755 challenge-173/roger-bell-west/python/ch-1.py create mode 100755 challenge-173/roger-bell-west/python/ch-2.py create mode 100755 challenge-173/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-173/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-173/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-173/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-173/roger-bell-west/rust/ch-1.rs create mode 100644 challenge-173/roger-bell-west/rust/ch-2.rs diff --git a/challenge-173/roger-bell-west/javascript/ch-1.js b/challenge-173/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..97ade88b5c --- /dev/null +++ b/challenge-173/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,60 @@ +#! /usr/bin/node + +"use strict" + +function esthetic10(n) { + return esthetic(n, 10); +} + +function esthetic(n0, base) { + let n = n0; + let pdigit = 0; + let ch = false; + while (n > 0) { + let digit = n % base; + if (ch && Math.abs(digit - pdigit) != 1) { + return false; + } + ch = true; + pdigit = digit; + n = Math.floor(n / base); + } + return true; +} + +if (esthetic10(5456)) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (!esthetic10(120)) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (esthetic10(12)) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (esthetic10(5654)) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +if (!esthetic10(890)) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); + +process.stdout.write("\n"); diff --git a/challenge-173/roger-bell-west/javascript/ch-2.js b/challenge-173/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..0b645e77cd --- /dev/null +++ b/challenge-173/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,56 @@ +#! /usr/bin/node + +"use strict" + +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0] + for( var key in a) count[0]++ + for( var key in b) count[1]++ + if( count[0]-count[1] != 0) {return false} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false} + } + return true + } + else + { + return a === b + } +} + + +function sylvester(ct) { + let o = [ 2n ]; + for (let i = 2; i <= ct; i++) { + let x = o[o.length - 1]; + o.push(1n + (x * (x - 1n))); + } + return o +} + +if (deepEqual(sylvester(10),[ + 2n, + 3n, + 7n, + 43n, + 1807n, + 3263443n, + 10650056950807n, + 113423713055421844361000443n, + 12864938683278671740537145998360961546653259485195807n, + 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443n +])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-173/roger-bell-west/kotlin/ch-1.kt b/challenge-173/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..bef12f0536 --- /dev/null +++ b/challenge-173/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,60 @@ +import kotlin.math.* + +fun esthetic10(n: Int): Boolean { + return esthetic(n, 10) +} + +fun esthetic(n0: Int, base: Int): Boolean { + var n = n0 + var pdigit = 0 + var ch = false + while (n > 0) { + var digit = n % base + if (ch && abs(digit - pdigit) != 1) { + return false + } + ch = true + pdigit = digit + n /= base + } + return true +} + +fun main() { + if (esthetic10(5456)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (!esthetic10(120)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (esthetic10(12)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (esthetic10(5654)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + if (!esthetic10(890)) { + print("Pass") + } else { + print("FAIL") + } + print(" ") + + println("") +} diff --git a/challenge-173/roger-bell-west/kotlin/ch-2.kt b/challenge-173/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..638813108c --- /dev/null +++ b/challenge-173/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,31 @@ +import java.math.BigInteger + +fun sylvester(ct: Int): ArrayList { + var o = arrayListOf(BigInteger("2")) + val one = BigInteger("1") + for (i in 2..ct) { + val x = o[o.size - 1] + o.add(one + (x * (x - one))) + } + return o + } + +fun main() { + if (sylvester(10) == listOf( + BigInteger("2"), + BigInteger("3"), + BigInteger("7"), + BigInteger("43"), + BigInteger("1807"), + BigInteger("3263443"), + BigInteger("10650056950807"), + BigInteger("113423713055421844361000443"), + BigInteger("12864938683278671740537145998360961546653259485195807"), + BigInteger("165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443") + )) { + print("Pass") + } else { + print("FAIL") + } + println("") +} diff --git a/challenge-173/roger-bell-west/lua/ch-1.lua b/challenge-173/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..baf1c4d59d --- /dev/null +++ b/challenge-173/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,56 @@ +#! /usr/bin/lua + +function esthetic10(n) + return esthetic(n, 10) +end + +function esthetic(n0, base) + local n = n0 + local pdigit + local ch = false + while n > 0 do + local digit = n % base + if ch and math.abs(digit - pdigit) ~= 1 then + return false + end + ch = true + pdigit = digit + n = math.floor(n / base) + end + return true +end + +if esthetic10(5456) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not esthetic10(120) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if esthetic10(12) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if esthetic10(5654) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if not esthetic10(890) then + io.write("Pass") +else + io.write("FAIL") +end +print("") diff --git a/challenge-173/roger-bell-west/perl/ch-1.pl b/challenge-173/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..0ac0c12198 --- /dev/null +++ b/challenge-173/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,47 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is(esthetic10(5456), + 1, + 'example 1'); + +is(esthetic10(120), + 0, + 'example 2'); + +is(esthetic10(12), + 1, + 'example 3'); + +is(esthetic10(5654), + 1, + 'example 4'); + +is(esthetic10(890), + 0, + 'example 5'); + +sub esthetic10($n) { + return esthetic($n,10); +} + +sub esthetic($n0,$base) { + my $n = $n0; + my $pdigit; + my $ch = 0; + while ($n > 0) { + my $digit = $n % $base; + if ($ch && abs($digit - $pdigit) != 1) { + return 0; + } + $ch = 1; + $pdigit = $digit; + $n = int($n / $base); + } + return 1; +} diff --git a/challenge-173/roger-bell-west/perl/ch-2.pl b/challenge-173/roger-bell-west/perl/ch-2.pl new file mode 100755 index 0000000000..21c4004b9e --- /dev/null +++ b/challenge-173/roger-bell-west/perl/ch-2.pl @@ -0,0 +1,32 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use bigint; + +use Test::More tests => 1; + +is_deeply(sylvester(10), + [ + 2, + 3, + 7, + 43, + 1807, + 3263443, + 10650056950807, + 113423713055421844361000443, + 12864938683278671740537145998360961546653259485195807, + 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443, + ], + 'example 1'); + +sub sylvester($ct) { + my @o = (2); + foreach (2..$ct) { + push @o,1 + ($o[-1] * ($o[-1]-1)) + } + return \@o; +} diff --git a/challenge-173/roger-bell-west/postscript/ch-1.ps b/challenge-173/roger-bell-west/postscript/ch-1.ps new file mode 100644 index 0000000000..2c16129d9b --- /dev/null +++ b/challenge-173/roger-bell-west/postscript/ch-1.ps @@ -0,0 +1,74 @@ +%!PS + +% begin included library code +% see https://github.com/Firedrake/postscript-libraries/ +/test { + /test.count test.count 1 add def + { + /test.pass test.pass 1 add def + } { + ( ) print + test.count (....) cvs print + (-fail) print + } ifelse +} bind def + +/test.end { + ( ) print + test.count 0 gt { + (Passed ) print + test.pass (...) cvs print + (/) print + test.count (...) cvs print + ( \() print + test.pass 100 mul test.count idiv (...) cvs print + (%\)) print + (\r\n) print + } if +} bind def + +/test.start { + print (:) print + /test.pass 0 def + /test.count 0 def +} bind def + + +% end included library code + +/esthetic10 { + 10 esthetic +} bind def + +/esthetic { + 5 dict begin + /base exch def + /n exch def + /pdigit 0 def + /ch false def + true + { + n 0 le { + exit + } if + /digit n base mod def + ch { + digit pdigit sub abs 1 ne { + pop false + exit + } if + } if + /ch true def + /pdigit digit def + /n n base idiv def + } loop + end +} bind def + +(esthetic) test.start +5456 esthetic10 test +120 esthetic10 not test +12 esthetic10 test +5654 esthetic10 test +890 esthetic10 not test +test.end diff --git a/challenge-173/roger-bell-west/python/ch-1.py b/challenge-173/roger-bell-west/python/ch-1.py new file mode 100755 index 0000000000..3ee4659182 --- /dev/null +++ b/challenge-173/roger-bell-west/python/ch-1.py @@ -0,0 +1,38 @@ +#! /usr/bin/python3 + +import unittest + +def esthetic(n0, base): + n = n0 + pdigit = 0 + ch = False + while n > 0: + digit = n % base + if ch and abs(digit - pdigit) != 1: + return False + ch = True + pdigit = digit + n //= base + return True + +def esthetic10(n): + return esthetic(n, 10) + +class TestEsthetic(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(esthetic10(5456),True,'example 1') + + def test_ex2(self): + self.assertEqual(esthetic10(120),False,'example 1') + + def test_ex3(self): + self.assertEqual(esthetic10(12),True,'example 1') + + def test_ex4(self): + self.assertEqual(esthetic10(5654),True,'example 1') + + def test_ex5(self): + self.assertEqual(esthetic10(890),False,'example 1') + +unittest.main() diff --git a/challenge-173/roger-bell-west/python/ch-2.py b/challenge-173/roger-bell-west/python/ch-2.py new file mode 100755 index 0000000000..08e12ca80e --- /dev/null +++ b/challenge-173/roger-bell-west/python/ch-2.py @@ -0,0 +1,27 @@ +#! /usr/bin/python3 + +import unittest + +def sylvester(ct): + o = [ 2 ] + for i in range(2,ct+1): + o.append(1 + (o[-1] * (o[-1] - 1))) + return o + +class TestSylvester(unittest.TestCase): + + def test_ex1(self): + self.assertEqual(sylvester(10),[ + 2, + 3, + 7, + 43, + 1807, + 3263443, + 10650056950807, + 113423713055421844361000443, + 12864938683278671740537145998360961546653259485195807, + 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443 + ],'example 1') + +unittest.main() diff --git a/challenge-173/roger-bell-west/raku/ch-1.p6 b/challenge-173/roger-bell-west/raku/ch-1.p6 new file mode 100755 index 0000000000..47ad65aee8 --- /dev/null +++ b/challenge-173/roger-bell-west/raku/ch-1.p6 @@ -0,0 +1,46 @@ +#! /usr/bin/perl6 + +use Test; + +plan 5; + +is(esthetic10(5456), + True, + 'example 1'); + +is(esthetic10(120), + False, + 'example 2'); + +is(esthetic10(12), + True, + 'example 3'); + +is(esthetic10(5654), + True, + 'example 4'); + +is(esthetic10(890), + False, + 'example 5'); + + +sub esthetic10($n) { + return esthetic($n,10); +} + +sub esthetic($n0,$base) { + my $n = $n0; + my $pdigit; + my $ch = False; + while ($n > 0) { + my $digit = $n % $base; + if ($ch && abs($digit - $pdigit) != 1) { + return False; + } + $ch = True; + $pdigit = $digit; + $n = floor($n / $base); + } + return True; +} diff --git a/challenge-173/roger-bell-west/raku/ch-2.p6 b/challenge-173/roger-bell-west/raku/ch-2.p6 new file mode 100755 index 0000000000..432242e501 --- /dev/null +++ b/challenge-173/roger-bell-west/raku/ch-2.p6 @@ -0,0 +1,28 @@ +#! /usr/bin/perl6 + +use Test; + +plan 1; + +is-deeply(sylvester(10), + [ + 2, + 3, + 7, + 43, + 1807, + 3263443, + 10650056950807, + 113423713055421844361000443, + 12864938683278671740537145998360961546653259485195807, + 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443, + ], + 'example 1'); + +sub sylvester($ct) { + my @o = (2); + for (2..$ct) { + @o.push(1 + (@o[*-1] * (@o[*-1]-1))); + } + return @o; +} diff --git a/challenge-173/roger-bell-west/ruby/ch-1.rb b/challenge-173/roger-bell-west/ruby/ch-1.rb new file mode 100755 index 0000000000..29d7f39ccd --- /dev/null +++ b/challenge-173/roger-bell-west/ruby/ch-1.rb @@ -0,0 +1,46 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def esthetic(n0, base) + n = n0 + pdigit = 0 + ch = false + while n > 0 do + n,digit = n.divmod(base) + if ch && (digit-pdigit).abs != 1 then + return false + end + ch = true + pdigit = digit + end + return true +end + +def esthetic10(n) + return esthetic(n, 10) +end + +class TestEsthetic < Test::Unit::TestCase + + def test_ex1 + assert_equal(true,esthetic10(5456)) + end + + def test_ex2 + assert_equal(false,esthetic10(120)) + end + + def test_ex3 + assert_equal(true,esthetic10(12)) + end + + def test_ex4 + assert_equal(true,esthetic10(5654)) + end + + def test_ex5 + assert_equal(false,esthetic10(890)) + end + +end diff --git a/challenge-173/roger-bell-west/ruby/ch-2.rb b/challenge-173/roger-bell-west/ruby/ch-2.rb new file mode 100755 index 0000000000..1b0a365f30 --- /dev/null +++ b/challenge-173/roger-bell-west/ruby/ch-2.rb @@ -0,0 +1,30 @@ +#! /usr/bin/ruby + +require 'test/unit' + +def sylvester(ct) + o = [ 2 ] + 2.upto(ct) do + o.push(1 + (o[-1] * (o[-1] - 1))) + end + return o +end + +class TestSylvester < Test::Unit::TestCase + + def test_ex1 + assert_equal([ + 2, + 3, + 7, + 43, + 1807, + 3263443, + 10650056950807, + 113423713055421844361000443, + 12864938683278671740537145998360961546653259485195807, + 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443 + ],sylvester(10)) + end + +end diff --git a/challenge-173/roger-bell-west/rust/ch-1.rs b/challenge-173/roger-bell-west/rust/ch-1.rs new file mode 100755 index 0000000000..15c280752e --- /dev/null +++ b/challenge-173/roger-bell-west/rust/ch-1.rs @@ -0,0 +1,47 @@ +#! /bin/sh +//usr/bin/env rustc --test $0 -o ${0}x && ./${0}x --nocapture; rm -f ${0}x ; exit + +#[test] +fn test_ex1() { + assert_eq!(esthetic10(5456), true); +} + +#[test] +fn test_ex2() { + assert_eq!(esthetic10(120), false); +} + +#[test] +fn test_ex3() { + assert_eq!(esthetic10(12), true); +} + +#[test] +fn test_ex4() { + assert_eq!(esthetic10(5654), true); +} + +#[test] +fn test_ex5() { + assert_eq!(esthetic10(890), false); +} + +fn esthetic10(n: i64) -> bool { + esthetic(n, 10) +} + +fn esthetic(n0: i64, base: i64) -> bool { + let mut n = n0; + let mut pdigit = 0; + let mut ch = false; + while n > 0 { + let digit = n % base; + if ch && (digit - pdigit).abs() != 1 { + return false; + } + ch = true; + pdigit = digit; + n /= base + } + return true; +} diff --git a/challenge-173/roger-bell-west/rust/ch-2.rs b/challenge-173/roger-bell-west/rust/ch-2.rs new file mode 100644 index 0000000000..0a411bb50e --- /dev/null +++ b/challenge-173/roger-bell-west/rust/ch-2.rs @@ -0,0 +1,28 @@ +use num_bigint::BigUint; + +#[test] +fn test_ex1() { + assert_eq!(sylvester(10), + vec![ + BigUint::from(2_u32), + BigUint::from(3_u32), + BigUint::from(7_u32), + BigUint::from(43_u32), + BigUint::from(1807_u32), + BigUint::from(3263443_u32), + BigUint::from(10650056950807_u64), + BigUint::from(113423713055421844361000443_u128), + "12864938683278671740537145998360961546653259485195807".parse::().unwrap(), + "165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443".parse::().unwrap() + ]); +} + +fn sylvester(ct: usize) -> Vec { + let mut o = vec![BigUint::from(2_u32)]; + let one = BigUint::from(1_u32); + for _ in 2..=ct { + let x = o.last().unwrap().clone(); + o.push(one.clone() + (x.clone() * (x - one.clone()))); + } + o +} -- cgit From 05723f74a9b61105baac75b265d42818b85edc52 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Fri, 15 Jul 2022 12:29:40 +0100 Subject: Blog post for challenge #173 --- challenge-173/roger-bell-west/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-173/roger-bell-west/blog.txt diff --git a/challenge-173/roger-bell-west/blog.txt b/challenge-173/roger-bell-west/blog.txt new file mode 100644 index 0000000000..914ecdcca8 --- /dev/null +++ b/challenge-173/roger-bell-west/blog.txt @@ -0,0 +1 @@ +https://blog.firedrake.org/archive/2022/07/The_Weekly_Challenge_173__The_Aesthetics_of_Sylvester.html -- cgit From cc2d9caa4c2bf2031a25f4e139ca1f1aa1923af5 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Tue, 12 Jul 2022 17:09:22 +0200 Subject: Solution to task 1 --- challenge-173/jo-37/perl/ch-1.pl | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100755 challenge-173/jo-37/perl/ch-1.pl diff --git a/challenge-173/jo-37/perl/ch-1.pl b/challenge-173/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..3857a34d7a --- /dev/null +++ b/challenge-173/jo-37/perl/ch-1.pl @@ -0,0 +1,83 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use Math::Prime::Util 'todigits'; +use List::Util 'reduce'; +use experimental 'signatures'; + +our ($tests, $examples, $base); +$base ||= 10; + +run_tests() if $tests || $examples; # does not return + +die < Date: Tue, 12 Jul 2022 17:09:36 +0200 Subject: Solution to task 2 --- challenge-173/jo-37/perl/ch-2.pl | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 challenge-173/jo-37/perl/ch-2.pl diff --git a/challenge-173/jo-37/perl/ch-2.pl b/challenge-173/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..d2757717c1 --- /dev/null +++ b/challenge-173/jo-37/perl/ch-2.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl -s + +use v5.16; +use bigint; +use Test2::V0; +# Need --force to install, see comment in week 168. +use List::Gen ':iterate'; + +our $examples; + +run_tests() if $examples; # does not return + +die <say(shift); + + +### Implementation + +# Build a generator for Sylvester's sequence using the recurrence +# relation starting with the value "2". +sub gen_sylvester { + iterate{$_ * ($_ - 1) + 1}->from(2); +} + + +### Examples and tests + +sub run_tests { + is gen_sylvester()->take(10), + [qw(2 3 7 43 1807 3263443 10650056950807 + 113423713055421844361000443 + 12864938683278671740537145998360961546653259485195807 + 165506647324519964198468195444439180017513152706377497841851388766535868639572406808911988131737645185443)], + 'task 2'; + + done_testing; + exit; +} -- cgit From 2dd4592441a9f370268396ef55bd11171e6c8cd9 Mon Sep 17 00:00:00 2001 From: Humberto Massa Date: Fri, 15 Jul 2022 13:44:48 -0300 Subject: Answers to challenge 173 by Humberto Massa --- challenge-173/massa/raku/ch-1.raku | 4 ++++ challenge-173/massa/raku/ch-2.raku | 3 +++ 2 files changed, 7 insertions(+) create mode 100644 challenge-173/massa/raku/ch-1.raku create mode 100644 challenge-173/massa/raku/ch-2.raku diff --git a/challenge-173/massa/raku/ch-1.raku b/challenge-173/massa/raku/ch-1.raku new file mode 100644 index 0000000000..f1a68ab26d --- /dev/null +++ b/challenge-173/massa/raku/ch-1.raku @@ -0,0 +1,4 @@ +use v6; +unit sub MAIN(Int $n); +my sub esthetic-number(Int $n --> Bool) { $n.comb.rotor(2 => -1).map({1 == abs [-] $_}).all.Bool } +say esthetic-number $n diff --git a/challenge-173/massa/raku/ch-2.raku b/challenge-173/massa/raku/ch-2.raku new file mode 100644 index 0000000000..b36eea2eb3 --- /dev/null +++ b/challenge-173/massa/raku/ch-2.raku @@ -0,0 +1,3 @@ +use v6.d; +unit sub MAIN(Int $n = 10); +say (2, 3, * × * + 1 ... *).head: $n -- cgit From 4e2d62b030546c9c04c8b9f355adff3cc0182d09 Mon Sep 17 00:00:00 2001 From: Walt Mankowski Date: Fri, 15 Jul 2022 21:17:16 -0400 Subject: fixed link syntax --- challenge-173/walt-mankowski/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-173/walt-mankowski/README.md b/challenge-173/walt-mankowski/README.md index 7a5af58509..b9e53c1a92 100644 --- a/challenge-173/walt-mankowski/README.md +++ b/challenge-173/walt-mankowski/README.md @@ -20,7 +20,7 @@ sub is_esthetic($n) { ## Task 2: Sylvester's Sequence -For this task we need to generate the first 10 terms of (Sylvester's Sequence)[https://en.wikipedia.org/wiki/Sylvester%27s_sequence]. **Sylvester's Sequence** is an integer sequence where every term is the product of all the previous terms, plus one. +For this task we need to generate the first 10 terms of [Sylvester's Sequence](https://en.wikipedia.org/wiki/Sylvester%27s_sequence). **Sylvester's Sequence** is an integer sequence where every term is the product of all the previous terms, plus one. Terms in Sylvester's Sequence get very large very fast (the 10th term has 20 digits) so we need to use the `bigint` module. I also used the `prod` function from `List::Util` to do the multiplication. With those modules in hand, we can solve this challenge in just a few lines of code: @@ -31,4 +31,4 @@ use List::Util qw(product); my @sylvester; push @sylvester, 1 + product @sylvester while @sylvester < 10; say for @sylvester; -``` \ No newline at end of file +``` -- cgit From 224ba471de9097133e40f89db7f784e734dd3219 Mon Sep 17 00:00:00 2001 From: Util Date: Fri, 15 Jul 2022 21:48:47 -0500 Subject: Add TWC 173 solutions by Bruce Gray : only Raku --- challenge-173/bruce-gray/raku/ch-1.raku | 37 +++++++++++++++++++++++++++++++++ challenge-173/bruce-gray/raku/ch-2.raku | 19 +++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 challenge-173/bruce-gray/raku/ch-1.raku create mode 100644 challenge-173/bruce-gray/raku/ch-2.raku diff --git a/challenge-173/bruce-gray/raku/ch-1.raku b/challenge-173/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..91f684c1d2 --- /dev/null +++ b/challenge-173/bruce-gray/raku/ch-1.raku @@ -0,0 +1,37 @@ +sub is-esthetic ( UInt $n --> Bool ) { + so $n.comb.rotor(2 => -1).map({ abs [-] .list }).all == 1; +} + +# Bonus: Efficent method to generate all esthetic numbers, +# without grepping through ℕ . +constant @all-esthetics = gather loop { + state @last_generation = 1..9; + .take for @last_generation; + my @next_generation = gather for @last_generation -> $i { + my $d = $i % 10; + my $n = $i * 10 + $d; + + take $n - 1 if $d !== 0; + take $n + 1 if $d !== 9; + } + @last_generation = @next_generation; +}; + + +multi sub MAIN ( UInt $n ) { + say is-esthetic $n; +