From 7ddeafed1ed79aaad252b3ca237c80fa0cddb618 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Fri, 31 Mar 2023 11:13:59 +0100 Subject: Add Perl solution --- challenge-175/paulo-custodio/Makefile | 2 + challenge-175/paulo-custodio/perl/ch-1.pl | 42 ++++++++++++++++++++ challenge-175/paulo-custodio/perl/ch-2.pl | 61 ++++++++++++++++++++++++++++++ challenge-175/paulo-custodio/t/test-1.yaml | 17 +++++++++ challenge-175/paulo-custodio/t/test-2.yaml | 5 +++ 5 files changed, 127 insertions(+) create mode 100644 challenge-175/paulo-custodio/Makefile create mode 100644 challenge-175/paulo-custodio/perl/ch-1.pl create mode 100644 challenge-175/paulo-custodio/perl/ch-2.pl create mode 100644 challenge-175/paulo-custodio/t/test-1.yaml create mode 100644 challenge-175/paulo-custodio/t/test-2.yaml diff --git a/challenge-175/paulo-custodio/Makefile b/challenge-175/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-175/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-175/paulo-custodio/perl/ch-1.pl b/challenge-175/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..7df64e1d95 --- /dev/null +++ b/challenge-175/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/perl + +# Challenge 175 +# +# Task 1: Last Sunday +# Submitted by: Mohammad S Anwar +# +# Write a script to list Last Sunday of every month in the given year. +# +# For example, for year 2022, we should get the following: +# +# 2022-01-30 +# 2022-02-27 +# 2022-03-27 +# 2022-04-24 +# 2022-05-29 +# 2022-06-26 +# 2022-07-31 +# 2022-08-28 +# 2022-09-25 +# 2022-10-30 +# 2022-11-27 +# 2022-12-25 + +use Modern::Perl; +use DateTime; + +sub last_sunday_month { + my($year, $month) = @_; + my $date = DateTime->last_day_of_month(year=>$year, month=>$month); + $date->add(days=>-1) while $date->day_of_week() != 7; + return $date; +} + +sub last_sunday_year { + my($year) = @_; + return map {last_sunday_month($year, $_)} 1..12; +} + +@ARGV==1 or die "usage: ch-1.pl year\n"; +my $year = shift; +say $_->ymd for last_sunday_year($year); diff --git a/challenge-175/paulo-custodio/perl/ch-2.pl b/challenge-175/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..bb1e537129 --- /dev/null +++ b/challenge-175/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl + +# Challenge 175 +# +# Task 2: Perfect Totient Numbers +# Submitted by: Mohammad S Anwar +# +# Write a script to generate first 20 Perfect Totient Numbers. Please checkout +# wikipedia page for more informations. +# +# Output +# +# 3, 9, 15, 27, 39, 81, 111, 183, 243, 255, 327, 363, 471, 729, +# 2187, 2199, 3063, 4359, 4375, 5571 + +use Modern::Perl; + +sub gcd { + my($a, $b) = @_; + if ($a == 0) { + return $b; + } + else { + return gcd($b % $a, $a); + } +} + +sub totient { + my($n) = @_; + my $count = 0; + for my $i (1..$n) { + $count++ if gcd($n, $i) == 1; + } + return $count; +} + +sub is_perfect_totient { + my($n) = @_; + my $sum = 0; + my $tot = $n; + while ($tot != 1) { + $tot = totient($tot); + $sum += $tot; + } + return $sum == $n; +} + +sub perfect_totients { + my($N) = @_; + my @result; + my $n = 1; + while (@result < $N) { + push @result, $n if is_perfect_totient($n); + $n++; + } + return @result; +} + +@ARGV==1 or die "usage: ch-2.pl N\n"; +my $N = shift; +say join ", ", perfect_totients($N); diff --git a/challenge-175/paulo-custodio/t/test-1.yaml b/challenge-175/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..8b5cb3ecb5 --- /dev/null +++ b/challenge-175/paulo-custodio/t/test-1.yaml @@ -0,0 +1,17 @@ +- setup: + cleanup: + args: 2022 + input: + output: | + |2022-01-30 + |2022-02-27 + |2022-03-27 + |2022-04-24 + |2022-05-29 + |2022-06-26 + |2022-07-31 + |2022-08-28 + |2022-09-25 + |2022-10-30 + |2022-11-27 + |2022-12-25 diff --git a/challenge-175/paulo-custodio/t/test-2.yaml b/challenge-175/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..1229c6897b --- /dev/null +++ b/challenge-175/paulo-custodio/t/test-2.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 20 + input: + output: 3, 9, 15, 27, 39, 81, 111, 183, 243, 255, 327, 363, 471, 729, 2187, 2199, 3063, 4359, 4375, 5571 -- cgit