aboutsummaryrefslogtreecommitdiff
path: root/challenge-150
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-02-03 21:09:47 +0000
committerGitHub <noreply@github.com>2022-02-03 21:09:47 +0000
commita720fbeef3a38892ab85d564c363f757254e1f85 (patch)
tree5dd3a6a1bdc19a964ee796d215a86440d74c8229 /challenge-150
parentbd279cc88842b2b3d46ce276d4487c1482e5d7f0 (diff)
parentfe7b3534323735602039b916f9c2b8735a1e98ae (diff)
downloadperlweeklychallenge-club-a720fbeef3a38892ab85d564c363f757254e1f85.tar.gz
perlweeklychallenge-club-a720fbeef3a38892ab85d564c363f757254e1f85.tar.bz2
perlweeklychallenge-club-a720fbeef3a38892ab85d564c363f757254e1f85.zip
Merge pull request #5608 from polettix/polettix/pwc150
Add polettix's solution to challenge-150
Diffstat (limited to 'challenge-150')
-rw-r--r--challenge-150/polettix/blog.txt1
-rw-r--r--challenge-150/polettix/blog1.txt1
-rw-r--r--challenge-150/polettix/perl/ch-1.pl31
-rw-r--r--challenge-150/polettix/perl/ch-2.pl32
-rw-r--r--challenge-150/polettix/raku/ch-1.raku33
-rw-r--r--challenge-150/polettix/raku/ch-2.raku28
6 files changed, 126 insertions, 0 deletions
diff --git a/challenge-150/polettix/blog.txt b/challenge-150/polettix/blog.txt
new file mode 100644
index 0000000000..fd01d159e1
--- /dev/null
+++ b/challenge-150/polettix/blog.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2022/02/02/pwc150-fibonacci-words/
diff --git a/challenge-150/polettix/blog1.txt b/challenge-150/polettix/blog1.txt
new file mode 100644
index 0000000000..6c54896efa
--- /dev/null
+++ b/challenge-150/polettix/blog1.txt
@@ -0,0 +1 @@
+https://github.polettix.it/ETOOBUSY/2022/02/03/pwc150-square-free-integer/
diff --git a/challenge-150/polettix/perl/ch-1.pl b/challenge-150/polettix/perl/ch-1.pl
new file mode 100644
index 0000000000..2540b80cc4
--- /dev/null
+++ b/challenge-150/polettix/perl/ch-1.pl
@@ -0,0 +1,31 @@
+#!/usr/bin/env perl
+use 5.024;
+use warnings;
+use English qw< -no_match_vars >;
+use experimental qw< postderef signatures >;
+no warnings qw< experimental::postderef experimental::signatures >;
+
+say "Fibonacci Words:\n";
+my $it = fibonacci_words_iterator(@ARGV);
+while ('necessary') {
+ my $item = $it->();
+ say "'$item'";
+ if (length $item >= 51) {
+ my $digit = substr $item, 50, 1;
+ say "\nThe 51st digit in the first term having at least 51 digits '$item' is $digit.";
+ last;
+ }
+}
+
+sub fibonacci_words_iterator ($f0, $f1) {
+ my @cache = ('', '', $f0, $f1);
+ my $backlog = 2;
+ return sub {
+ if (! $backlog) {
+ ($f0, $f1) = ($f1, $f0 . $f1);
+ return $f1;
+ }
+ --$backlog;
+ return $backlog ? $f0 : $f1;
+ };
+}
diff --git a/challenge-150/polettix/perl/ch-2.pl b/challenge-150/polettix/perl/ch-2.pl
new file mode 100644
index 0000000000..02c037e5f4
--- /dev/null
+++ b/challenge-150/polettix/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+use 5.024;
+use warnings;
+use English qw< -no_match_vars >;
+use experimental qw< postderef signatures >;
+no warnings qw< experimental::postderef experimental::signatures >;
+
+if (@ARGV > 1) {
+ say $_, ' ', (is_square_free($_) ? 'is' : 'is not'), ' square free'
+ for @ARGV;
+}
+else {
+ my $limit = shift // 500;
+ my @list = grep { is_square_free($_) } 1 .. $limit;
+ while (@list) {
+ print join ', ', splice @list, 0, 20;
+ say @list ? ',' : '';
+ }
+}
+
+sub is_square_free ($N) {
+ return unless $N % 4;
+ my $divisor = 3;
+ while ($N > $divisor) {
+ if ($N % $divisor == 0) {
+ $N /= $divisor;
+ return unless $N % $divisor;
+ }
+ $divisor += 2; # go through odd candidates only
+ }
+ return 1;
+}
diff --git a/challenge-150/polettix/raku/ch-1.raku b/challenge-150/polettix/raku/ch-1.raku
new file mode 100644
index 0000000000..f37cf06124
--- /dev/null
+++ b/challenge-150/polettix/raku/ch-1.raku
@@ -0,0 +1,33 @@
+#!/usr/bin/env raku
+use v6;
+
+class FibonacciWords { ... };
+
+sub MAIN (Int:D $a, Int:D $b where $b.Str.chars == $a.Str.chars) {
+ my $it = FibonacciWords.new($a, $b);
+ put "Fibonacci Words\n";
+ loop {
+ my $item = $it.next();
+ put "'$item'";
+ if $item.chars >= 51 {
+ my $digit = $item.substr(50, 1);
+ put "\nThe 51st digit in the first term having at least 51 digits '$item' is $digit.";
+ last;
+ }
+ }
+}
+
+class FibonacciWords {
+ has $!f0 is built;
+ has $!f1 is built;
+ has $!backlog = 2;
+ method new (Str() $f0, Str() $f1) { self.bless(:$f0, :$f1) }
+ method next () {
+ if ! $!backlog {
+ ($!f0, $!f1) = ($!f1, $!f0 ~ $!f1);
+ return $!f1;
+ }
+ --$!backlog;
+ return $!backlog ?? $!f0 !! $!f1;
+ }
+}
diff --git a/challenge-150/polettix/raku/ch-2.raku b/challenge-150/polettix/raku/ch-2.raku
new file mode 100644
index 0000000000..35a29749b0
--- /dev/null
+++ b/challenge-150/polettix/raku/ch-2.raku
@@ -0,0 +1,28 @@
+#!/usr/bin/env raku
+use v6;
+
+multi sub MAIN (Int $limit = 500) {
+ my @list = (1 .. $limit).grep({is-square-free($_)});
+ while @list {
+ @list.splice(0, 20).join(', ').print;
+ put @list ?? ',' !! '';
+ }
+}
+
+multi sub MAIN (*@args) {
+ put $_, ' ', (is-square-free($_) ?? 'is' !! 'is not'), ' square free'
+ for @args;
+}
+
+sub is-square-free ($N is copy) {
+ return False if $N %% 4;
+ my $divisor = 3;
+ while $N > $divisor {
+ if $N %% $divisor == 0 {
+ $N = ($N / $divisor).Int;
+ return False if $N %% $divisor;
+ }
+ $divisor += 2; # go through odd candidates only
+ }
+ return True;
+}