From bb0bfdd4256087e61d645bd7e989b4a7eb39c314 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Sat, 8 Jan 2022 12:47:15 +0000 Subject: Task 1 & 2 --- challenge-146/perlboy1967/perl/ch-1.pl | 27 ++++++++++++++++++++ challenge-146/perlboy1967/perl/ch-2.pl | 46 ++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 challenge-146/perlboy1967/perl/ch-1.pl create mode 100755 challenge-146/perlboy1967/perl/ch-2.pl diff --git a/challenge-146/perlboy1967/perl/ch-1.pl b/challenge-146/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..efe3ddbe44 --- /dev/null +++ b/challenge-146/perlboy1967/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 146 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-146/#TASK1 + +Author: Niels 'PerlBoy' van Dijke + +TASK #1 › 10001st Prime Number +Submitted by: Mohammad S Anwar + +Write a script to generate the 10001st prime number. + +=cut + +use v5.16; + +use Math::Primality qw(next_prime); + +my $N = shift // 10_001; + +my ($i,$p) = (1,2); + +$p = next_prime($p) while ($i++ < $N); + +printf "%d'th prime = %d\n", $N, $p; diff --git a/challenge-146/perlboy1967/perl/ch-2.pl b/challenge-146/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..8485fbef06 --- /dev/null +++ b/challenge-146/perlboy1967/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 146 + - https://perlweeklychallenge.org/blog/perl-weekly-challenge-146/#TASK2 + +Author: Niels 'PerlBoy' van Dijke + +TASK #2 › Curious Fraction Tree +Submitted by: Mohammad S Anwar + +You are given a fraction, member of the tree created similar to the above sample. + +Write a script to find out the parent and grandparent of the given member. +Example 1: + + Input: $member = '3/5'; + Output: parent = '3/2' and grandparent = '1/2' + +Example 2: + + Input: $member = '4/3'; + Output: parent = '1/3' and grandparent = '1/2' + +=cut + +use v5.16; + +my $m = shift // '3/5'; +my $p = getCFTparent($m); +my $g = getCFTparent($p); + +printf "member = '%s' has parent = '%s' and grandparent = '%s'\n", $m, $p, $g; + +sub getCFTparent($) { + my ($n) = @_; + + ($a, $b) = split(/\//, $n); + + if ($a < $b) { + return sprintf('%d/%d', $a, $b - $a); + } elsif ($a > $b) { + return sprintf('%d/%d', $a - $b, $b); + } +} -- cgit