diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-08-13 16:26:23 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2019-08-13 16:26:23 +0100 |
| commit | fa7fbcd8d9c668b0af0bd8b6a25424dbffec8909 (patch) | |
| tree | b93a984dd18d83a5d05897559d5ab52792076f63 /challenge-021 | |
| parent | 8b995a0d4595a8460cc97ef8d81f92717bf1b8ff (diff) | |
| download | perlweeklychallenge-club-fa7fbcd8d9c668b0af0bd8b6a25424dbffec8909.tar.gz perlweeklychallenge-club-fa7fbcd8d9c668b0af0bd8b6a25424dbffec8909.tar.bz2 perlweeklychallenge-club-fa7fbcd8d9c668b0af0bd8b6a25424dbffec8909.zip | |
- Added solutions by Duane Powell.
Diffstat (limited to 'challenge-021')
| -rw-r--r-- | challenge-021/duane-powell/perl5/ch-1.pl | 77 | ||||
| -rw-r--r-- | challenge-021/duane-powell/perl5/ch-2.pl | 41 |
2 files changed, 118 insertions, 0 deletions
diff --git a/challenge-021/duane-powell/perl5/ch-1.pl b/challenge-021/duane-powell/perl5/ch-1.pl new file mode 100644 index 0000000000..eee3e3a3df --- /dev/null +++ b/challenge-021/duane-powell/perl5/ch-1.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl +use Modern::Perl; +use bignum qw(e); +use Math::BigFloat; +Math::BigFloat->precision(-50); + +# write a script to calculate e https://en.wikipedia.org/wiki/E_(mathematical_constant) +# e, Euler's number, 2.71828182845904523536028747135266249775724709369995 + +sub factorial { + my $f = 1; + map { $f *= $_ } ( 1 .. shift ); + return $f; +} + +sub e_factorial { + my $e = 0; + map { $e += 1/factorial($_) } ( 0 .. shift ); + return $e; +} + +sub brothers { + my $b = 0; + map { $b += (2 * $_ + 2) / factorial(2 * $_ + 1) } ( 0 .. shift ); + return $b; +} + +sub e_brothers { + my $b = shift; + my $e = brothers($b); + return $e; +} + +sub e_limit_n { + my $n = shift; + return ( 1 + 1/$n ) ** $n; +} + +sub e_limit_t { + my $t = shift; + return ( 1 + $t ) ** (1/$t); +} + + +my $e = 2.71828182845904523536028747135266249775724709369995; +say $e, " e constant from wiki"; + +$e = e(); +say $e, " e from perl module bignum"; + +my $n = 40; +$e = e_factorial($n); +say $e, " e calculated from $n factorial"; + +$n = 20; +$e = e_brothers($n); +say $e, " e calculated from brothers, $n steps"; + +$n = 100000; +$e = e_limit_n($n); +say $e, " e calculated as a limit n approaching infinity $n"; + +$n = .00001; +$e = e_limit_t($n); +say $e, " e calculated as a limit t approaching zero $n"; + + +__END__ + +./ch1.pl +2.71828182845904523536028747135266249775724709369995 e constant from wiki +2.71828182845904523536028747135266249775700000000000 e from perl module bignum +2.71828182845904523536028747135266249775724709369992 e calculated from 40 factorial +2.71828182845904523536028747135266249775724709369997 e calculated from brothers, 20 steps +2.71826823717448966803506482442604644797444693267782 e calculated as a limit n approaching infinity 100000 +2.71826823717448966803506482442604644797444693267782 e calculated as a limit t approaching zero 0.00001 + diff --git a/challenge-021/duane-powell/perl5/ch-2.pl b/challenge-021/duane-powell/perl5/ch-2.pl new file mode 100644 index 0000000000..fccd7934d9 --- /dev/null +++ b/challenge-021/duane-powell/perl5/ch-2.pl @@ -0,0 +1,41 @@ +#!/usr/bin/perl +use Modern::Perl; +use URI qw(canonical); + +# take a list of urls and say their normal forms + +my @url = qw( +HTTP://www.Example.com/ +HTTP://www.example.COM:80/ +https://www.example.com/ +https://www.example.com:443/ +http://www.example.com/a%c2%b1b +http://www.example.com/%7Eusername/ +http://www.example.com:80/bar.html +); + +my %normal_url; +foreach my $url (@url) { + push @{ $normal_url{ URI->new($url)->canonical() } }, $url; +} + +foreach my $url ( sort {$a cmp $b} (keys %normal_url) ) { + say $url; + foreach ( sort {$a cmp $b} @{ $normal_url{$url} } ) { + say "\t$_"; + } +} + +__END__ + +./ch2.pl +http://www.example.com/ + HTTP://www.Example.com/ + http://www.example.com:80/ +http://www.example.com/a%C2%B1b + http://www.example.com/a%c2%b1b +http://www.example.com/bar.html + http://www.example.com:80/bar.html +http://www.example.com/~username/ + http://www.example.com/%7Eusername/ + |
