diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-04 17:22:49 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2022-04-04 17:22:49 +0100 |
| commit | 72b9ae6794ce48ed1047879ae943008437fceace (patch) | |
| tree | 9961d5cba5269c982d9483e2fd74706609cebf78 /challenge-159 | |
| parent | 6d1d4fab6f6f2a764988af40d5b00e0eaa4854df (diff) | |
| download | perlweeklychallenge-club-72b9ae6794ce48ed1047879ae943008437fceace.tar.gz perlweeklychallenge-club-72b9ae6794ce48ed1047879ae943008437fceace.tar.bz2 perlweeklychallenge-club-72b9ae6794ce48ed1047879ae943008437fceace.zip | |
Add Perl solutions
Diffstat (limited to 'challenge-159')
| -rw-r--r-- | challenge-159/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/README | 1 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/perl/ch-1.pl | 43 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/perl/ch-2.pl | 26 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-159/paulo-custodio/t/test-2.yaml | 15 |
6 files changed, 102 insertions, 0 deletions
diff --git a/challenge-159/paulo-custodio/Makefile b/challenge-159/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-159/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-159/paulo-custodio/README b/challenge-159/paulo-custodio/README new file mode 100644 index 0000000000..87dc0b2fbd --- /dev/null +++ b/challenge-159/paulo-custodio/README @@ -0,0 +1 @@ +Solution by Paulo Custodio diff --git a/challenge-159/paulo-custodio/perl/ch-1.pl b/challenge-159/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..de8cf8b75c --- /dev/null +++ b/challenge-159/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/env perl + +# Challenge 159 +# +# TASK #1 › Farey Sequence +# Submitted by: Mohammad S Anwar +# You are given a positive number, $n. +# +# Write a script to compute Farey Sequence of the order $n. +# +# Example 1: +# Input: $n = 5 +# Output: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1. +# Example 2: +# Input: $n = 7 +# Output: 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1. +# Example 3: +# Input: $n = 4 +# Output: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1. + +use Modern::Perl; +use ntheory qw( gcd ); + +my $n = shift || 1; +say join(", ", farey_sequence($n)); + +sub farey_sequence { + my($n) = @_; + my @seq = ([0,1], [1,1]); # first and last terms + + for my $i (1..$n) { + for my $j ($i+1..$n) { + if (gcd($i, $j)==1) { + push @seq, [$i,$j]; + } + } + } + + @seq = sort { $a->[0]/$a->[1] <=> $b->[0]/$b->[1] } @seq; + @seq = map {$_->[0].'/'.$_->[1]} @seq; + + return @seq; +} diff --git a/challenge-159/paulo-custodio/perl/ch-2.pl b/challenge-159/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..0b8df98a0b --- /dev/null +++ b/challenge-159/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +# Challenge 159 +# +# TASK #2 › Moebius Number +# Submitted by: Mohammad S Anwar +# You are given a positive number $n. +# +# Write a script to generate the Moebius Number for the given number. +# Please refer to wikipedia page for more informations. +# +# Example 1: +# Input: $n = 5 +# Output: -1 +# Example 2: +# Input: $n = 10 +# Output: 1 +# Example 3: +# Input: $n = 20 +# Output: 0 + +use Modern::Perl; +use ntheory qw( moebius ); + +my $n = shift || 1; +say moebius($n); diff --git a/challenge-159/paulo-custodio/t/test-1.yaml b/challenge-159/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..fe5e1a56ee --- /dev/null +++ b/challenge-159/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 4 + input: + output: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1 +- setup: + cleanup: + args: 5 + input: + output: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1 +- setup: + cleanup: + args: 7 + input: + output: 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1 diff --git a/challenge-159/paulo-custodio/t/test-2.yaml b/challenge-159/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..146c50f10c --- /dev/null +++ b/challenge-159/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 5 + input: + output: -1 +- setup: + cleanup: + args: 10 + input: + output: 1 +- setup: + cleanup: + args: 20 + input: + output: 0 |
