diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2020-12-29 23:49:46 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2020-12-29 23:49:46 +0000 |
| commit | 61443d8d8458247954fcefebafe9a3081fa91fcf (patch) | |
| tree | 9dc32e11a6dd164ec219886bfe2009c802ac955b /challenge-006 | |
| parent | 7aa3eaa643de75db34ae4e6322b4a98831a91051 (diff) | |
| download | perlweeklychallenge-club-61443d8d8458247954fcefebafe9a3081fa91fcf.tar.gz perlweeklychallenge-club-61443d8d8458247954fcefebafe9a3081fa91fcf.tar.bz2 perlweeklychallenge-club-61443d8d8458247954fcefebafe9a3081fa91fcf.zip | |
Add Perl solution to challenge 006
and fix way of getting PI with n digits in challenge 004.1
Diffstat (limited to 'challenge-006')
| -rw-r--r-- | challenge-006/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-006/paulo-custodio/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-006/paulo-custodio/perl/ch-2.pl | 20 | ||||
| -rw-r--r-- | challenge-006/paulo-custodio/test.pl | 19 |
4 files changed, 62 insertions, 0 deletions
diff --git a/challenge-006/paulo-custodio/README b/challenge-006/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-006/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-006/paulo-custodio/perl/ch-1.pl b/challenge-006/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..da74575996 --- /dev/null +++ b/challenge-006/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl + +# Challenge 006 +# +# Challenge #1 +# Create a script which takes a list of numbers from command line and print the same in the compact form. For example, if you pass “1,2,3,4,9,10,14,15,16” then it should print the compact form like “1-4,9,10,14-16”. + +use strict; +use warnings; +use 5.030; + +my @nums = sort {$a<=>$b} split /\D+/, "@ARGV"; +while (@nums) { + my $n = shift @nums; + print $n; + if (@nums>1 && $nums[0]==$n+1 && $nums[1]==$n+2) { + $n = shift @nums while @nums && $nums[0]==$n+1; + print "-$n"; + } + print "," if @nums; +} +print "\n"; diff --git a/challenge-006/paulo-custodio/perl/ch-2.pl b/challenge-006/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..8a5f4bf914 --- /dev/null +++ b/challenge-006/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl + +# Challenge 006 +# +# Challenge #2 +# Create a script to calculate Ramanujan’s constant with at least 32 digits of precision. Find out more about it here. +# +# The standard IEEE 754 double-precision binary floating-point format: binary64 gives only 15 to 17 significant decimal digits +# Therefore must use the bignum library + +use strict; +use warnings; +use 5.030; +use Math::BigFloat; + +my $accuracy = 64; + +my $e = Math::BigFloat->bpi($accuracy) * Math::BigFloat->bsqrt(163, $accuracy); +my $k = Math::BigFloat->bexp($e, $accuracy); +say $k; diff --git a/challenge-006/paulo-custodio/test.pl b/challenge-006/paulo-custodio/test.pl new file mode 100644 index 0000000000..49705094f2 --- /dev/null +++ b/challenge-006/paulo-custodio/test.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Test::More; +use 5.030; + +is capture("perl perl/ch-1.pl 1,2,3,4,9,10,14,15,16"), "1-4,9,10,14-16\n"; + +is capture("perl perl/ch-2.pl"), "262537412640768743.9999999999992500725971981856888793538563373316\n"; + +done_testing; + +sub capture { + my($cmd) = @_; + my $out = `$cmd`; + $out =~ s/[ \t\v\f\r]*\n/\n/g; + return $out; +} |
