aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2022-02-02 16:50:32 -0500
committerDave Jacoby <jacoby.david@gmail.com>2022-02-02 16:50:32 -0500
commit4501a6b5f5eec7ec46543dd55e833619cd850a5e (patch)
treeb4f26450b869b025d66b34b0bf69c16f7e24743a
parent6fd6b4eaedb3b19ae8d913ba078c9d69f48227dd (diff)
downloadperlweeklychallenge-club-4501a6b5f5eec7ec46543dd55e833619cd850a5e.tar.gz
perlweeklychallenge-club-4501a6b5f5eec7ec46543dd55e833619cd850a5e.tar.bz2
perlweeklychallenge-club-4501a6b5f5eec7ec46543dd55e833619cd850a5e.zip
150
-rw-r--r--challenge-150/dave-jacoby/blog.txt1
-rw-r--r--challenge-150/dave-jacoby/perl/ch-1.pl23
-rw-r--r--challenge-150/dave-jacoby/perl/ch-2.pl56
3 files changed, 80 insertions, 0 deletions
diff --git a/challenge-150/dave-jacoby/blog.txt b/challenge-150/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..335a83c54f
--- /dev/null
+++ b/challenge-150/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2022/02/02/free-the-squares-the-weekly-challenge-150.html
diff --git a/challenge-150/dave-jacoby/perl/ch-1.pl b/challenge-150/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..bb0cff0455
--- /dev/null
+++ b/challenge-150/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental };
+
+use Getopt::Long;
+use List::Util qw{ sum0 max };
+
+say fibonacci_words( '1234', '5678' );
+
+sub fibonacci_words ( $word1, $word2 ) {
+ my @words;
+ push @words, $word1, $word2;
+ while ( length $words[-1] < 51 ) {
+ my $w = $words[-2] . $words[-1];
+ push @words, $w;
+ }
+ my $last = pop @words;
+ # zero indexing leads to possible fencepost error
+ return substr $last, 50, 1;
+}
diff --git a/challenge-150/dave-jacoby/perl/ch-2.pl b/challenge-150/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..3a48412911
--- /dev/null
+++ b/challenge-150/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental };
+
+use Term::ReadKey;
+
+display_pretty( square_free_integers() );
+
+sub square_free_integers () {
+ my @sfi;
+ my $max = 500;
+
+OUTER: for my $i ( 1 .. $max ) {
+ my @factors = factors($i);
+ for my $f (@factors) {
+ my $g = () = grep { /$f/ } @factors;
+ next OUTER if $g > 1;
+ }
+ push @sfi, $i;
+ }
+
+ return @sfi;
+}
+
+sub factors ( $n ) {
+ my @factors;
+ my $i = 2;
+ while ( $i < $n ) {
+ while ( $n % $i == 0 ) {
+ $n /= $i;
+ push @factors, $i;
+ }
+ $i++;
+ }
+ return @factors;
+}
+
+sub display_pretty( @arr ) {
+ my ( $wchar, undef ) = GetTerminalSize();
+ $wchar //= 80;
+ my $line;
+
+ while ( scalar @arr > 1 ) {
+ my $n = shift @arr;
+ $line .= qq{$n, };
+ if ( (length $line )+ 5 > $wchar ) {
+ say $line;
+ $line = '';
+ }
+ }
+ $line .= shift @arr;
+ say $line;
+}