From d83b768b10245ee334907efea43a6a5a0820062e Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 1 May 2019 16:00:08 +0100 Subject: - Added solutions by Athanasius. --- challenge-006/athanasius/perl5/ch-1.pl | 46 ++++++++++++++++++++++++++++++++++ challenge-006/athanasius/perl5/ch-2.pl | 23 +++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 challenge-006/athanasius/perl5/ch-1.pl create mode 100644 challenge-006/athanasius/perl5/ch-2.pl diff --git a/challenge-006/athanasius/perl5/ch-1.pl b/challenge-006/athanasius/perl5/ch-1.pl new file mode 100644 index 0000000000..e916b409dd --- /dev/null +++ b/challenge-006/athanasius/perl5/ch-1.pl @@ -0,0 +1,46 @@ +use strict; +use warnings; + +if (@ARGV != 1 || $ARGV[0] =~ /[^\d,+-]/) +{ + print "\nUsage: perl $0 < comma-separated integer list (no spaces) >\n"; + exit 0; +} + +my @numbers = split /,/, $ARGV[0]; +my $last = shift @numbers; +my @runs = ( [ $last ] ); + +while (my $n = shift @numbers) +{ + if ($n == $last + 1) + { + push $runs[-1]->@*, $n; + } + else + { + push @runs, [ $n ]; + } + + $last = $n; +} + +my @ranges; + +for my $run (@runs) +{ + if (scalar @$run > 2) + { + push @ranges, $run->[0] . '-' . $run->[-1]; + } + else + { + push @ranges, join(',', @$run); + } +} + +print "\n", join(',', @ranges), "\n"; + +__END__ + +0:37 >perl ch-1.pl 1,2,3,4,9,10,14,15,16,18 diff --git a/challenge-006/athanasius/perl5/ch-2.pl b/challenge-006/athanasius/perl5/ch-2.pl new file mode 100644 index 0000000000..4445fb287e --- /dev/null +++ b/challenge-006/athanasius/perl5/ch-2.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; +use Const::Fast; +use Math::BigFloat; + +const my $INT_DIGITS => 18; +const my $HEEGNER => 163; +const my $PRECISION => 33; +const my $ACCURACY => $INT_DIGITS + $PRECISION + 3; + +Math::BigFloat->accuracy($ACCURACY); + +my $squareroot = Math::BigFloat->new($HEEGNER)->bsqrt(); # sqrt(163) +my $ramanujan = Math::BigFloat->bpi(); # pi +$ramanujan->bmul($squareroot); # pi * sqrt(163) +$ramanujan->bexp(); # e^(pi * sqrt(163)) +$ramanujan->bfround(-$PRECISION); # to 33 decimal places + +print "\n$ramanujan\n"; + +__END__ + +0:37 >perl ch-2.pl -- cgit