aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-18 18:12:28 +0000
committerGitHub <noreply@github.com>2022-01-18 18:12:28 +0000
commit1f9e5b191608452b7edbe1f63db2e1f9011f6ee5 (patch)
treec1e2ee099e5f80b7e06b8448f6ba7bb3a549247f
parent9dec2d33063ebd736823310cf12e80006740e690 (diff)
parent07bf35dae32a2d4b30f0aff83531461a9454187d (diff)
downloadperlweeklychallenge-club-1f9e5b191608452b7edbe1f63db2e1f9011f6ee5.tar.gz
perlweeklychallenge-club-1f9e5b191608452b7edbe1f63db2e1f9011f6ee5.tar.bz2
perlweeklychallenge-club-1f9e5b191608452b7edbe1f63db2e1f9011f6ee5.zip
Merge pull request #5537 from choroba/ech148
Add solutions to 148: Eban Numbers & Cardano Triplets by E. Choroba
-rwxr-xr-xchallenge-148/e-choroba/perl/ch-1.pl26
-rwxr-xr-xchallenge-148/e-choroba/perl/ch-2.pl45
2 files changed, 71 insertions, 0 deletions
diff --git a/challenge-148/e-choroba/perl/ch-1.pl b/challenge-148/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..1fcada37b9
--- /dev/null
+++ b/challenge-148/e-choroba/perl/ch-1.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+my $is_eban;
+if (eval { require Lingua::EN::Numbers }) {
+ $is_eban = sub { Lingua::EN::Numbers::num2en($_[0]) !~ /e/ };
+
+} elsif (eval { require Number::Spell }) {
+ $is_eban = sub { Number::Spell::spell_number($_[0]) !~ /e/ };
+
+} else {
+ $is_eban = sub {
+ my ($n) = @_;
+ return if $n =~ /[12789].$/
+ || $n =~ /[135789]$/
+ || $n == 0
+ || $n =~ /[^0]..$/;
+ return $is_eban->(substr $n, 0, -3)
+ if 3 < length $n; # thousand .. nonillion
+ return 1
+ };
+}
+
+say join ', ', grep $is_eban->($_), 0 .. 100;
diff --git a/challenge-148/e-choroba/perl/ch-2.pl b/challenge-148/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..e453b21073
--- /dev/null
+++ b/challenge-148/e-choroba/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+# https://stackoverflow.com/a/70414782/1030675
+sub cardano_triplets {
+ my ($count) = @_;
+ for (my $u = 1; $count--; $u += 2) {
+ my $A = (1 + 3 * $u) / 2;
+ my $t = $u * $u * $u + $A * $A;
+
+ my $B = int sqrt $t;
+ --$B while $t % ($B * $B);
+
+ my $C = $t / ($B * $B);
+
+ say "$A $B $C";
+ }
+}
+
+# This finds all the triplets, but they aren't sorted so nicely.
+sub cardano_triplets_all {
+ my ($count) = @_;
+ for (my $u = 1;; $u += 2) {
+ my $A = (1 + 3 * $u) / 2;
+ my $t = $u * $u * $u + $A * $A;
+
+ my $B = int sqrt $t;
+ while (1) {
+ --$B while $B && $t % ($B * $B);
+ last unless $B;
+
+ my $C = $t / ($B * $B);
+ say "$A $B $C";
+ return unless --$count;
+
+ --$B;
+ }
+ }
+}
+
+cardano_triplets(5);
+say '-' x 10;
+cardano_triplets_all(5);