diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-04-10 15:51:18 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-04-10 15:51:18 +0100 |
| commit | b338f1334ce67fbbdf66b7e036f1e2827ac706a2 (patch) | |
| tree | db0435222295593c4e2603eb496463f267f19fe7 /challenge-159 | |
| parent | 0d8c23b8719d25825d059bf133b8f2ac147efa45 (diff) | |
| download | perlweeklychallenge-club-b338f1334ce67fbbdf66b7e036f1e2827ac706a2.tar.gz perlweeklychallenge-club-b338f1334ce67fbbdf66b7e036f1e2827ac706a2.tar.bz2 perlweeklychallenge-club-b338f1334ce67fbbdf66b7e036f1e2827ac706a2.zip | |
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-159')
| -rwxr-xr-x | challenge-159/pete-houston/perl/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-159/pete-houston/perl/ch-2.pl | 25 |
2 files changed, 59 insertions, 0 deletions
diff --git a/challenge-159/pete-houston/perl/ch-1.pl b/challenge-159/pete-houston/perl/ch-1.pl new file mode 100755 index 0000000000..29598c0570 --- /dev/null +++ b/challenge-159/pete-houston/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 15901.pl +# +# USAGE: ./15901.pl N +# +# DESCRIPTION: Print the Farey Sequence of order N +# +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 06/04/22 +#=============================================================================== + +use strict; +use warnings; + +my $n = shift; +die "Argument must be natural number.\n" + unless defined ($n) && $n =~ /^[1-9][0-9]*$/; + +# Initialise the sequence +my ($fn, $fd, $ln, $ld) = (0, 1, 1, $n); +my @seq = ("$fn/$fd", "$ln/$ld"); + +# Loop over the rest +while ($ln < $n) { + my $x = int (($n + $fd) / $ld); + ($fn, $fd, $ln, $ld) = ($ln, $ld, $x * $ln - $fn, $x * $ld - $fd); + push @seq, "$ln/$ld" if $ld >= $ln; +} + +print "@seq\n"; diff --git a/challenge-159/pete-houston/perl/ch-2.pl b/challenge-159/pete-houston/perl/ch-2.pl new file mode 100755 index 0000000000..da15c155e1 --- /dev/null +++ b/challenge-159/pete-houston/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 15902.pl +# +# USAGE: ./15902.pl N +# +# DESCRIPTION: Output the Möbius number for N +# +# REQUIREMENTS: Math::Prime::Util +# NOTES: It is amazing how simple Perl with Math::Prime::UTil +# makes this. +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 06/04/22 +#=============================================================================== + +use strict; +use warnings; +use feature 'say'; + +use Math::Prime::Util 'moebius'; + +say moebius shift; |
