diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-01-09 19:38:28 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-01-09 19:38:28 +0000 |
| commit | bf0e37317122de0322d69b1c68bd07ccfc5b36a8 (patch) | |
| tree | 5031b453f163650bf08f28093e65bc5aa6873eab /challenge-146 | |
| parent | 543c00b7aedce99655017c3920914302df649995 (diff) | |
| download | perlweeklychallenge-club-bf0e37317122de0322d69b1c68bd07ccfc5b36a8.tar.gz perlweeklychallenge-club-bf0e37317122de0322d69b1c68bd07ccfc5b36a8.tar.bz2 perlweeklychallenge-club-bf0e37317122de0322d69b1c68bd07ccfc5b36a8.zip | |
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-146')
| -rwxr-xr-x | challenge-146/pete-houston/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-146/pete-houston/perl/ch-2.pl | 96 |
2 files changed, 118 insertions, 0 deletions
diff --git a/challenge-146/pete-houston/perl/ch-1.pl b/challenge-146/pete-houston/perl/ch-1.pl new file mode 100755 index 0000000000..78477cbaa7 --- /dev/null +++ b/challenge-146/pete-houston/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 14601.pl +# +# USAGE: ./14601.pl [ N ] +# +# DESCRIPTION: Output the Nth prime (defaults to 10001) +# +# OPTIONS: N defaults to 10_001 if unspecified +# REQUIREMENTS: Math::Prime::Util, what else? +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 05/01/22 +#=============================================================================== + +use strict; +use warnings; +use Math::Prime::Util 'nth_prime'; + +print nth_prime (shift // 10_001) . "\n"; diff --git a/challenge-146/pete-houston/perl/ch-2.pl b/challenge-146/pete-houston/perl/ch-2.pl new file mode 100755 index 0000000000..eafd0a7e2c --- /dev/null +++ b/challenge-146/pete-houston/perl/ch-2.pl @@ -0,0 +1,96 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 14602.pl +# +# USAGE: ./14602.pl N/M +# +# DESCRIPTION: Print out 2 ancestors of the tree value matching the +# argument. +# +# REQUIREMENTS: Class::Tiny +# NOTES: The tree is really unnecessary here. See 14602b.pl for +# alternative approach without one. +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 03/01/22 +#=============================================================================== + +use strict; +use warnings; + +# Each pair of children have values which are: +# Left: parent numerator over sum of parent numerator and denominator +# Right: sum of parent numerator and denominator over parent denominator + +package Node; + +use Class::Tiny qw/lchild rchild parent value/; + +sub BUILD { + + #my ($class, $value, $r, $maxdepth, $parent) = @_; + my ($self, $args) = @_; + my $r = $args->{row}; + my $maxdepth = $args->{maxdepth}; + + # Build tree from 1/1 + unless ($args->{row}) { + $r //= 0; + $self->value ('1/1') unless defined $self->value; + } + + # Add children if we are not the bottom row + if ($r < $maxdepth) { + my ($num, $dom) = split /\//, $self->value; + my $sum = $num + $dom; + my %childargs = ( + row => $r + 1, + maxdepth => $maxdepth, + parent => $self + ); + $self->lchild (Node->new (value => "$num/$sum", %childargs)); + $self->rchild (Node->new (value => "$sum/$dom", %childargs)); + } + +} + +# Retrieve or calculate the minimum path downwards from here. +sub find_by_value { + my ($self, $value) = @_; + return $self if $self->value eq $value; + for my $child ($self->lchild, $self->rchild) { + next unless defined $child; + my $res = $child->find_by_value ($value); + return $res if defined $res; + } + return; +} + +package main; + +my $root = Node->new (value => '1/1', row => 0, maxdepth => 4); + +my $v = shift; +my $one = $root->find_by_value ($v); + +unless (defined $one) { + print "No match for value '$v' found\n"; + exit; +} + +my $parent = $one->parent; +unless (defined $parent) { + print "No parent found\n"; + exit; +} +print "parent = '" . $parent->value . "' and "; + +my $gparent = $parent->parent; +unless (defined $gparent) { + print "no grandparent found\n"; + exit; +} +print "grandparent = '" . $gparent->value . "'\n"; + |
