aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2019-08-07 20:55:00 +0200
committerLubos Kolouch <lubos@kolouch.net>2019-08-07 20:55:00 +0200
commitdf743ffb9fedee75ddfe5b378ac110ca76399807 (patch)
treef225b31bf3c8a7b61d8885151f8e78284a9d6d6c
parent619c631409cce47e1e115aa95813780c8085fd67 (diff)
downloadperlweeklychallenge-club-df743ffb9fedee75ddfe5b378ac110ca76399807.tar.gz
perlweeklychallenge-club-df743ffb9fedee75ddfe5b378ac110ca76399807.tar.bz2
perlweeklychallenge-club-df743ffb9fedee75ddfe5b378ac110ca76399807.zip
Challenge 020 LK
-rw-r--r--challenge-020/lubos-kolouch/perl5/ch-1.pl78
-rw-r--r--challenge-020/lubos-kolouch/perl5/ch-2.pl81
2 files changed, 159 insertions, 0 deletions
diff --git a/challenge-020/lubos-kolouch/perl5/ch-1.pl b/challenge-020/lubos-kolouch/perl5/ch-1.pl
new file mode 100644
index 0000000000..dcfae744a3
--- /dev/null
+++ b/challenge-020/lubos-kolouch/perl5/ch-1.pl
@@ -0,0 +1,78 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-1.pl
+#
+# USAGE: ./ch-1.pl
+#
+# DESCRIPTION: Task #1
+#
+# Write a script to accept a string from command line and split it on change of character. For example, if the string is “ABBCDEEF”, then it should split like “A”, “BB”, “C”, “D”, “EE”, “F”.
+#
+# OPTIONS: ---
+# REQUIREMENTS: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Lubos Kolouch,
+# ORGANIZATION:
+# VERSION: 1.0
+# CREATED: 08/05/2019 08:48:27 PM
+# REVISION: ---
+#===============================================================================
+
+use strict;
+use warnings;
+use feature qw{ say };
+use Carp;
+
+sub split_str {
+ my $input = shift;
+
+ # remember the previous character
+ my $previous_char;
+
+ # store the end result
+ my $result;
+
+ # process all characters in the string
+ for my $char ( split //msx, $input ) {
+
+ # if the current char is different than previous, let's split
+ $result .= "\n" if ($previous_char) and ( $char ne $previous_char );
+
+ # add current char to result
+ $result .= $char;
+
+ # let's remember the current character
+ $previous_char = $char;
+
+ }
+
+ return $result;
+}
+
+###### MAIN ######
+
+my $input = shift or croak 'Usage: script string';
+
+say split_str($input);
+
+###### TESTS ######
+
+use Test::More;
+
+say 'TESTS:';
+
+# test the sample
+like( split_str('ABBCDEEF'), qr/A\nBB\nC\nD\nEE\nF/msx, 'test ABBCDEEF' );
+unlike( split_str('ABBCDEEF'), qr/A\nBB\nC\nD\nEE\nAF/msx, 'test wrong ABBCDEEF' );
+
+# two chars at the beginning
+like( split_str('AABBCDEEF'), qr/AA\nBB\nC\nD\nEE\nF/msx, 'test AABBCDEEF' );
+unlike( split_str('AABBCDEEF'), qr/AA\nBB\nC\nD\nEE\nAF/msx, 'test wrong AABBCDEEF' );
+
+# two chars at the end
+like( split_str('ABBCDEEFF'), qr/A\nBB\nC\nD\nEE\nFF/msx, 'test ABBCDEEFF' );
+unlike( split_str('ABBCDEEFF'), qr/A\nBB\nC\nD\nEE\nAFF/msx, 'test wrong ABBCDEEFF' );
+
+done_testing;
diff --git a/challenge-020/lubos-kolouch/perl5/ch-2.pl b/challenge-020/lubos-kolouch/perl5/ch-2.pl
new file mode 100644
index 0000000000..edc674ad09
--- /dev/null
+++ b/challenge-020/lubos-kolouch/perl5/ch-2.pl
@@ -0,0 +1,81 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-2.pl
+#
+# USAGE: ./ch-2.pl
+#
+# DESCRIPTION: Task #2
+#
+# Task #2
+# Write a script to print the smallest pair of Amicable Numbers. For more information, please checkout wikipedia page.
+#
+#
+# OPTIONS: ---
+# REQUIREMENTS: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Lubos Kolouch,
+# ORGANIZATION:
+# VERSION: 1.0
+# CREATED: 07/08/2019 08:48:27 PM
+# REVISION: ---
+#===============================================================================
+
+use strict;
+use warnings;
+use feature qw{ say };
+use Math::Prime::XS qw/is_prime/;
+use utf8;
+
+sub get_amicable {
+ my $n = shift;
+
+ # Thābit ibn Qurra theorem
+ my $p = 3 * 2**( $n - 1 ) - 1;
+ return 0 unless is_prime($p);
+
+ my $q = 3 * 2**($n) - 1;
+ return 0 unless is_prime($q);
+
+ my $r = 9 * 2**( 2 * $n - 1 ) - 1;
+ return 0 unless is_prime($r);
+
+ return ( 2**($n) * $p * $q, 2**($n) * $r );
+}
+
+###### MAIN ######
+
+# must start with n>1
+my $candidate = 2;
+
+while (1) {
+ if ( my (@result) = get_amicable($candidate) ) {
+ say 'Pair found: ' . join qq/,/, @result;
+ last;
+ }
+
+ $candidate++;
+}
+
+###### TESTS ######
+
+use Test::More;
+
+my @got = get_amicable(2);
+my @expected = ( 220, 284 );
+is_deeply( \@got, \@expected, 'Test 2' );
+
+@got = get_amicable(3);
+@expected = (0);
+is_deeply( \@got, \@expected, 'Test 3' );
+
+@got = get_amicable(4);
+@expected = ( 17_296, 18_416 );
+is_deeply( \@got, \@expected, 'Test 4' );
+
+@got = get_amicable(5);
+@expected = (0);
+is_deeply( \@got, \@expected, 'Test 5' );
+
+done_testing;