aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2021-03-07 19:46:26 +0000
committerNiels van Dijke <perlboy@cpan.org>2021-03-07 19:46:26 +0000
commitc6fa04bf0fe4e9154b5219dd6aba14efcf3e6392 (patch)
tree61fde3962c74f51abf831cac14ec67d14ef48c8b
parent83ca98c65aae1882774dc3eb55e0dfc0a5914135 (diff)
downloadperlweeklychallenge-club-c6fa04bf0fe4e9154b5219dd6aba14efcf3e6392.tar.gz
perlweeklychallenge-club-c6fa04bf0fe4e9154b5219dd6aba14efcf3e6392.tar.bz2
perlweeklychallenge-club-c6fa04bf0fe4e9154b5219dd6aba14efcf3e6392.zip
Task 1 & 2
-rwxr-xr-xchallenge-102/perlboy1967/perl/ch-1.pl97
-rwxr-xr-xchallenge-102/perlboy1967/perl/ch-2.pl34
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-102/perlboy1967/perl/ch-1.pl b/challenge-102/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..1aad958dfa
--- /dev/null
+++ b/challenge-102/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,97 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 102
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-102/#TASK1
+#
+# Task 1 - Rare Numbers
+#
+# Author: Niels 'PerlBoy' van Dijke
+#
+# Note: This is the first time I use MCE.
+# Possibly I use it not optimally ;-)
+
+use v5.16;
+use strict;
+use warnings;
+
+use MCE;
+use MCE::Util;
+
+use POSIX qw(ceil);
+use Time::HiRes qw(gettimeofday tv_interval);
+use List::Util qw(min);
+
+@ARGV = (9)
+ unless (scalar @ARGV);
+
+my $N = shift @ARGV;
+
+die "Please provide integer number >= 1"
+ unless ($N =~ m#^\d+$# and $N >= 1);
+
+my %rep = (
+ 1 => 2,
+ 3 => 4,
+ 5 => 6,
+ 7 => 8,
+ 9 => 20,
+);
+
+my ($r0,$r1) = (1,1);
+
+my $t0 = [gettimeofday];
+
+my @results;
+
+my $mce = MCE->new(
+ chunk_size => 10_000,
+ max_workers => MCE::Util::get_ncpu(),
+ user_func => sub {
+ my ($mce, $chunk_ref, $chunk_id) = @_;
+
+ foreach (@$chunk_ref) {
+ if (isRare($_)) {
+ my $elapsed = tv_interval ($t0);
+ printf "%6.3f rare: %d\n", $elapsed, $_;
+ }
+ }
+ }
+);
+
+do {
+ $r0++;
+
+ # Small optimization
+ # (skip all numbers which start with an odd number)
+ my $d = substr($r0,0,1);
+ if (exists $rep{$d}) {
+ substr($r0,0,1,$rep{$d});
+ }
+
+ # Create dynamic chunks with a maximum of 1M
+ # (to keep memory utilisation in control)
+ my $step = min(1_000_000, 10**ceil(log($r0)/log(10)) - 1 - $r0);
+ $mce->process([ $r0 .. $r0+$step ]);
+ $r0 += $step;
+
+} until ($r0 > 10**$N);
+
+
+sub isRare {
+ my $r1 = int(reverse($_[0]));
+
+ # Palindrome number cannot be rare number
+ return 0 if ($r1 == $_[0]);
+
+ my $d = $_[0] - $r1;
+ return 0 if ($d <= 0);
+ my $sqrtD = sqrt($d);
+ return 0 if ($sqrtD != int($sqrtD));
+
+ my $s = $_[0] + $r1;
+ my $sqrtS = sqrt($s);
+ return 0 if ($sqrtS != int($sqrtS));
+
+ return 1;
+}
+
diff --git a/challenge-102/perlboy1967/perl/ch-2.pl b/challenge-102/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..4689fa80a9
--- /dev/null
+++ b/challenge-102/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 102
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-102/#TASK2
+#
+# Task 2 - Hash-counting String
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use v5.16;
+use strict;
+use warnings;
+
+@ARGV = (<>)
+ unless (scalar @ARGV);
+
+foreach my $n (@ARGV) {
+ printf "hashCountingString(%d) = '%s'\n",
+ $n, hashCountingString($n);
+}
+
+
+sub hashCountingString {
+ my ($n) = @_;
+
+ my $r = '';
+
+ while ($n > 0) {
+ $r = ($n > 1 ? $n.'#'.$r : '#'.$r);
+ $n -= length($n) + 1;
+ }
+
+ return $r;
+}