diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-05-01 16:00:08 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-05-01 16:00:08 +0100 |
| commit | d83b768b10245ee334907efea43a6a5a0820062e (patch) | |
| tree | bdeef6331b5e427e8c57526209f1938a783ab16b /challenge-006/athanasius | |
| parent | 011f8de58d776118b9cc682b6e171ed4d6e8b324 (diff) | |
| download | perlweeklychallenge-club-d83b768b10245ee334907efea43a6a5a0820062e.tar.gz perlweeklychallenge-club-d83b768b10245ee334907efea43a6a5a0820062e.tar.bz2 perlweeklychallenge-club-d83b768b10245ee334907efea43a6a5a0820062e.zip | |
- Added solutions by Athanasius.
Diffstat (limited to 'challenge-006/athanasius')
| -rw-r--r-- | challenge-006/athanasius/perl5/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-006/athanasius/perl5/ch-2.pl | 23 |
2 files changed, 69 insertions, 0 deletions
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 |
