aboutsummaryrefslogtreecommitdiff
path: root/challenge-149
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-31 01:35:57 +0000
committerGitHub <noreply@github.com>2022-01-31 01:35:57 +0000
commit67a32e9bfdfe6232f3a2cdd68cf756e40156f7e0 (patch)
tree3d3075dacd522c608dd0739fa24cf0e1f43ca4a6 /challenge-149
parent599f50245a3a151da8b91acf0429a73e2e684884 (diff)
parent64b1f2ee1564d0814af671c76e40652f0282fdef (diff)
downloadperlweeklychallenge-club-67a32e9bfdfe6232f3a2cdd68cf756e40156f7e0.tar.gz
perlweeklychallenge-club-67a32e9bfdfe6232f3a2cdd68cf756e40156f7e0.tar.bz2
perlweeklychallenge-club-67a32e9bfdfe6232f3a2cdd68cf756e40156f7e0.zip
Merge pull request #5587 from arnesom/branch-for-challenge-149
Arne Sommer
Diffstat (limited to 'challenge-149')
-rw-r--r--challenge-149/arne-sommer/blog.txt1
-rwxr-xr-xchallenge-149/arne-sommer/perl/ch-1.pl58
-rwxr-xr-xchallenge-149/arne-sommer/perl/ch-2.pl41
-rwxr-xr-xchallenge-149/arne-sommer/perl/fibonacci-digit-sum-perl58
-rwxr-xr-xchallenge-149/arne-sommer/perl/largest-square-perl41
-rwxr-xr-xchallenge-149/arne-sommer/raku/ch-1.raku33
-rwxr-xr-xchallenge-149/arne-sommer/raku/ch-2.raku24
-rwxr-xr-xchallenge-149/arne-sommer/raku/fibonacci-digit-sum33
-rwxr-xr-xchallenge-149/arne-sommer/raku/largest-square25
-rwxr-xr-xchallenge-149/arne-sommer/raku/largest-square-ok24
10 files changed, 338 insertions, 0 deletions
diff --git a/challenge-149/arne-sommer/blog.txt b/challenge-149/arne-sommer/blog.txt
new file mode 100644
index 0000000000..7953d807e0
--- /dev/null
+++ b/challenge-149/arne-sommer/blog.txt
@@ -0,0 +1 @@
+https://raku-musings.com/fibonacci-square.html
diff --git a/challenge-149/arne-sommer/perl/ch-1.pl b/challenge-149/arne-sommer/perl/ch-1.pl
new file mode 100755
index 0000000000..3cbc7b1d85
--- /dev/null
+++ b/challenge-149/arne-sommer/perl/ch-1.pl
@@ -0,0 +1,58 @@
+#! /usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use feature 'signatures';
+use feature 'state';
+
+use Getopt::Long;
+use List::Util 'sum';
+
+no warnings qw(experimental::signatures);
+
+my $verbose = 0;
+
+GetOptions("verbose" => \$verbose);
+
+my $N = shift(@ARGV) // 20;
+my $current = 0;
+my @values;
+
+while (1)
+{
+ push(@values, $current) if is_fibonacci_sum($current);
+
+ last if @values == $N;
+
+ $current++;
+}
+
+say "f($N)=[" . join(", ", @values) . "]";
+
+sub is_fibonacci_sum ($number)
+{
+ state %is_fibonacci = ( 0 => 1, 1 => 1 );
+ state $limit = 1;
+ state $fib_1 = 0;
+ state $fib_2 = 1;
+
+ my $sum = sum split(//, $number);
+
+ say ": Considering number $number with sum $sum" if $verbose;
+
+ return 1 if $is_fibonacci{$sum};
+
+ while ($sum > $limit)
+ {
+ my $new = $fib_1 + $fib_2;
+ $fib_1 = $fib_2;
+ $fib_2 = $new;
+ $limit = $new;
+
+ say ": Caching Fibonacci number $limit" if $verbose;
+ $is_fibonacci{$limit} = 1;
+ }
+
+ return $is_fibonacci{$sum};
+}
diff --git a/challenge-149/arne-sommer/perl/ch-2.pl b/challenge-149/arne-sommer/perl/ch-2.pl
new file mode 100755
index 0000000000..07fab91dd0
--- /dev/null
+++ b/challenge-149/arne-sommer/perl/ch-2.pl
@@ -0,0 +1,41 @@
+#! /usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use feature 'signatures';
+
+use Getopt::Long;
+use Algorithm::Combinatorics 'permutations';
+use Math::Base::Convert;
+
+no warnings qw(experimental::signatures);
+
+my $verbose = 0;
+
+GetOptions("verbose" => \$verbose);
+
+my $base = shift(@ARGV);
+
+die "Illegal base." unless $base =~ /^\d+$/;
+die "Illegal base. Use 2..36 only" if $base < 2 or $base > 36;
+
+my @digits = ( 0..9,'A'..'Z' )[0 .. $base -1];
+
+my $converter = new Math::Base::Convert(\@digits, 10);
+
+for my $permutation (reverse permutations(\@digits))
+{
+ my $candidate = join("", @$permutation);
+
+ my $decimal = $converter->cnv($candidate);
+ my $sqrt = sqrt $decimal;
+
+ say ": Checking $candidate (decimal: $decimal root: $sqrt)" if $verbose;
+
+ if ($sqrt =~ /^\d+$/)
+ {
+ substr($candidate, 0,1) eq "0" ? say substr($candidate, 1) : say $candidate;
+ last;
+ }
+}
diff --git a/challenge-149/arne-sommer/perl/fibonacci-digit-sum-perl b/challenge-149/arne-sommer/perl/fibonacci-digit-sum-perl
new file mode 100755
index 0000000000..3cbc7b1d85
--- /dev/null
+++ b/challenge-149/arne-sommer/perl/fibonacci-digit-sum-perl
@@ -0,0 +1,58 @@
+#! /usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use feature 'signatures';
+use feature 'state';
+
+use Getopt::Long;
+use List::Util 'sum';
+
+no warnings qw(experimental::signatures);
+
+my $verbose = 0;
+
+GetOptions("verbose" => \$verbose);
+
+my $N = shift(@ARGV) // 20;
+my $current = 0;
+my @values;
+
+while (1)
+{
+ push(@values, $current) if is_fibonacci_sum($current);
+
+ last if @values == $N;
+
+ $current++;
+}
+
+say "f($N)=[" . join(", ", @values) . "]";
+
+sub is_fibonacci_sum ($number)
+{
+ state %is_fibonacci = ( 0 => 1, 1 => 1 );
+ state $limit = 1;
+ state $fib_1 = 0;
+ state $fib_2 = 1;
+
+ my $sum = sum split(//, $number);
+
+ say ": Considering number $number with sum $sum" if $verbose;
+
+ return 1 if $is_fibonacci{$sum};
+
+ while ($sum > $limit)
+ {
+ my $new = $fib_1 + $fib_2;
+ $fib_1 = $fib_2;
+ $fib_2 = $new;
+ $limit = $new;
+
+ say ": Caching Fibonacci number $limit" if $verbose;
+ $is_fibonacci{$limit} = 1;
+ }
+
+ return $is_fibonacci{$sum};
+}
diff --git a/challenge-149/arne-sommer/perl/largest-square-perl b/challenge-149/arne-sommer/perl/largest-square-perl
new file mode 100755
index 0000000000..07fab91dd0
--- /dev/null
+++ b/challenge-149/arne-sommer/perl/largest-square-perl
@@ -0,0 +1,41 @@
+#! /usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use feature 'signatures';
+
+use Getopt::Long;
+use Algorithm::Combinatorics 'permutations';
+use Math::Base::Convert;
+
+no warnings qw(experimental::signatures);
+
+my $verbose = 0;
+
+GetOptions("verbose" => \$verbose);
+
+my $base = shift(@ARGV);
+
+die "Illegal base." unless $base =~ /^\d+$/;
+die "Illegal base. Use 2..36 only" if $base < 2 or $base > 36;
+
+my @digits = ( 0..9,'A'..'Z' )[0 .. $base -1];
+
+my $converter = new Math::Base::Convert(\@digits, 10);
+
+for my $permutation (reverse permutations(\@digits))
+{
+ my $candidate = join("", @$permutation);
+
+ my $decimal = $converter->cnv($candidate);
+ my $sqrt = sqrt $decimal;
+
+ say ": Checking $candidate (decimal: $decimal root: $sqrt)" if $verbose;
+
+ if ($sqrt =~ /^\d+$/)
+ {
+ substr($candidate, 0,1) eq "0" ? say substr($candidate, 1) : say $candidate;
+ last;
+ }
+}
diff --git a/challenge-149/arne-sommer/raku/ch-1.raku b/challenge-149/arne-sommer/raku/ch-1.raku
new file mode 100755
index 0000000000..9856502cfe
--- /dev/null
+++ b/challenge-149/arne-sommer/raku/ch-1.raku
@@ -0,0 +1,33 @@
+#! /usr/bin/env raku
+
+subset PositiveInt of Int where * >= 1;
+
+unit sub MAIN (PositiveInt $N = 20, :v(:$verbose));
+
+my $fibonacci := (0, 1, * + * ... *);
+
+my $fds := (0 .. Inf).grep( *.&is-fibonacci-sum );
+
+say "f($N)=[", $fds[^$N].join(", "), "]";
+
+sub is-fibonacci-sum (Int $number)
+{
+ state %is-fibonacci;
+ state $limit = 0;
+ state $index = 0;
+
+ my $sum = $number.comb.sum;
+
+ say ": Considering number $number with sum $sum" if $verbose;
+
+ return True if %is-fibonacci{$sum};
+
+ while $sum >= $limit
+ {
+ $limit = $fibonacci[$index++];
+ say ": Caching Fibonacci number $limit" if $verbose;
+ %is-fibonacci{$limit} = True;
+ }
+
+ return %is-fibonacci{$sum};
+}
diff --git a/challenge-149/arne-sommer/raku/ch-2.raku b/challenge-149/arne-sommer/raku/ch-2.raku
new file mode 100755
index 0000000000..6819ffbf2f
--- /dev/null
+++ b/challenge-149/arne-sommer/raku/ch-2.raku
@@ -0,0 +1,24 @@
+#! /usr/bin/env raku
+
+subset Base of UInt where 1 < * <= 36;
+
+unit sub MAIN (Base $base, :v(:$verbose));
+
+my @digits = (( 0 .. 9, 'A' .. 'Z' ).flat)[0 .. $base -1];
+
+
+for @digits.permutations.reverse -> @permutation
+{
+ my $candidate = @permutation.join;
+
+ my $decimal = $candidate.parse-base($base);
+ my $sqrt = $decimal.sqrt;
+
+ say ": Checking $candidate (decimal: $decimal root: $sqrt)" if $verbose;
+
+ if $sqrt ~~ /^\d+$/
+ {
+ say $candidate.substr(0,1) eq "0" ?? $candidate.substr(1) !! $candidate;
+ last;
+ }
+}
diff --git a/challenge-149/arne-sommer/raku/fibonacci-digit-sum b/challenge-149/arne-sommer/raku/fibonacci-digit-sum
new file mode 100755
index 0000000000..9856502cfe
--- /dev/null
+++ b/challenge-149/arne-sommer/raku/fibonacci-digit-sum
@@ -0,0 +1,33 @@
+#! /usr/bin/env raku
+
+subset PositiveInt of Int where * >= 1;
+
+unit sub MAIN (PositiveInt $N = 20, :v(:$verbose));
+
+my $fibonacci := (0, 1, * + * ... *);
+
+my $fds := (0 .. Inf).grep( *.&is-fibonacci-sum );
+
+say "f($N)=[", $fds[^$N].join(", "), "]";
+
+sub is-fibonacci-sum (Int $number)
+{
+ state %is-fibonacci;
+ state $limit = 0;
+ state $index = 0;
+
+ my $sum = $number.comb.sum;
+
+ say ": Considering number $number with sum $sum" if $verbose;
+
+ return True if %is-fibonacci{$sum};
+
+ while $sum >= $limit
+ {
+ $limit = $fibonacci[$index++];
+ say ": Caching Fibonacci number $limit" if $verbose;
+ %is-fibonacci{$limit} = True;
+ }
+
+ return %is-fibonacci{$sum};
+}
diff --git a/challenge-149/arne-sommer/raku/largest-square b/challenge-149/arne-sommer/raku/largest-square
new file mode 100755
index 0000000000..9eed00f442
--- /dev/null
+++ b/challenge-149/arne-sommer/raku/largest-square
@@ -0,0 +1,25 @@
+#! /usr/bin/env raku
+
+subset Base of UInt where 1 < * <= 36;
+
+unit sub MAIN (Base $base, :v(:$verbose));
+
+my @digits = (( 0 .. 9, 'A' .. 'Z' ).flat)[0 .. $base -1];
+
+
+for @digits.permutations.reverse -> @permutation
+{
+ # last if @permutation[0] eq "0";
+ my $candidate = @permutation.join;
+
+ my $decimal = $candidate.parse-base($base);
+ my $sqrt = $decimal.sqrt;
+
+ say ": Checking $candidate (decimal: $decimal root: $sqrt)" if $verbose;
+
+ if $sqrt ~~ /^\d+$/
+ {
+ say $candidate;
+ last;
+ }
+}
diff --git a/challenge-149/arne-sommer/raku/largest-square-ok b/challenge-149/arne-sommer/raku/largest-square-ok
new file mode 100755
index 0000000000..6819ffbf2f
--- /dev/null
+++ b/challenge-149/arne-sommer/raku/largest-square-ok
@@ -0,0 +1,24 @@
+#! /usr/bin/env raku
+
+subset Base of UInt where 1 < * <= 36;
+
+unit sub MAIN (Base $base, :v(:$verbose));
+
+my @digits = (( 0 .. 9, 'A' .. 'Z' ).flat)[0 .. $base -1];
+
+
+for @digits.permutations.reverse -> @permutation
+{
+ my $candidate = @permutation.join;
+
+ my $decimal = $candidate.parse-base($base);
+ my $sqrt = $decimal.sqrt;
+
+ say ": Checking $candidate (decimal: $decimal root: $sqrt)" if $verbose;
+
+ if $sqrt ~~ /^\d+$/
+ {
+ say $candidate.substr(0,1) eq "0" ?? $candidate.substr(1) !! $candidate;
+ last;
+ }
+}