diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-11-08 17:39:03 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-11-08 17:39:03 +0000 |
| commit | ff7475c4559b1087f402725a7c526d3f780e3434 (patch) | |
| tree | 52495c7853bf31c427ec26f500c66c5bed6753b4 /challenge-085 | |
| parent | d7b448400532fdf43db9c26aeb4e7cc36d2de4a6 (diff) | |
| download | perlweeklychallenge-club-ff7475c4559b1087f402725a7c526d3f780e3434.tar.gz perlweeklychallenge-club-ff7475c4559b1087f402725a7c526d3f780e3434.tar.bz2 perlweeklychallenge-club-ff7475c4559b1087f402725a7c526d3f780e3434.zip | |
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-085')
| -rw-r--r-- | challenge-085/pete-houston/perl/ch-1.pl | 47 | ||||
| -rw-r--r-- | challenge-085/pete-houston/perl/ch-2.pl | 32 |
2 files changed, 79 insertions, 0 deletions
diff --git a/challenge-085/pete-houston/perl/ch-1.pl b/challenge-085/pete-houston/perl/ch-1.pl new file mode 100644 index 0000000000..2647337677 --- /dev/null +++ b/challenge-085/pete-houston/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 8501.pl +# +# USAGE: ./8501.pl N1 N2 ... +# +# DESCRIPTION: Given an array of positive floats determine if a sum +# of any three of them may fall between 1 and 2 +# +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 04/11/20 +#=============================================================================== + +use strict; +use warnings; + +my $ge = 1; +my $lt = 2; +my @valid = grep { $_ < $lt } @ARGV; +if ($#valid < 2) { + print "0\n"; + exit; +} + +find_sum (3, $ge, $lt, @valid); +print "0\n"; + +sub find_sum { + my ($count, $ge, $lt, @num) = @_; + if ($count < 2) { + my $res = grep { $_ >= $ge && $_ < $lt } @num; + if ($res) { + print "1\n"; + exit; + } + return; + } + for my $i (0 .. $#num) { + my $diff = $num[$i]; + my @new = @num; + splice (@new, $i, 1); + find_sum ($count - 1, $ge - $diff, $lt - $diff, @new); + } +} diff --git a/challenge-085/pete-houston/perl/ch-2.pl b/challenge-085/pete-houston/perl/ch-2.pl new file mode 100644 index 0000000000..e6dfb242d7 --- /dev/null +++ b/challenge-085/pete-houston/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 8502.pl +# +# USAGE: ./8502.pl N +# +# DESCRIPTION: Determine if whole number N can be expressed as an integer power +# +# REQUIREMENTS: Math::GMP +# NOTES: Gives details of which root and power match +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 04/11/20 +#=============================================================================== + +use strict; +use warnings; + +use Math::GMP; + +my $target = Math::GMP->new (shift @ARGV); +for my $power (2 .. $target) { + my ($root, $rem) = $target->brootrem ($power); + unless ($rem) { + print "$root ** $power = $target\n1\n"; + exit; + } + last if $root < 2; +} +print "0\n"; |
