aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-19 10:34:37 +0000
committerGitHub <noreply@github.com>2022-01-19 10:34:37 +0000
commitf3478e19ce01285dda27ee5d01db2006b49e4965 (patch)
tree6a8950c3025f8ff18d48396d0c62b1897e73a3a9
parentc0adc3a91ca7f5ba8d840c6897a946c184db65da (diff)
parent0442cd8a85d8849d7b61bf5908460d4a562350a1 (diff)
downloadperlweeklychallenge-club-f3478e19ce01285dda27ee5d01db2006b49e4965.tar.gz
perlweeklychallenge-club-f3478e19ce01285dda27ee5d01db2006b49e4965.tar.bz2
perlweeklychallenge-club-f3478e19ce01285dda27ee5d01db2006b49e4965.zip
Merge pull request #5543 from jacoby/master
Challenge 148
-rw-r--r--challenge-148/dave-jacoby/blog.txt1
-rw-r--r--challenge-148/dave-jacoby/perl/ch-1.pl19
-rw-r--r--challenge-148/dave-jacoby/perl/ch-2.pl48
3 files changed, 68 insertions, 0 deletions
diff --git a/challenge-148/dave-jacoby/blog.txt b/challenge-148/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..73299f8ca6
--- /dev/null
+++ b/challenge-148/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2022/01/19/its-triplets-weekly-challenge-148.html
diff --git a/challenge-148/dave-jacoby/perl/ch-1.pl b/challenge-148/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..bf62ab2981
--- /dev/null
+++ b/challenge-148/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,19 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental };
+
+use Lingua::EN::Numbers qw( num2en );
+
+# You COULD try to make up a way to do this, but this wheel has been
+# invented already and is sufficiently round.
+
+my @numbers;
+for my $i ( 0 .. 100 ) {
+ my $e = num2en $i;
+ next if $e =~ /e/mx;
+ push @numbers, $i;
+}
+say join ', ', @numbers;
diff --git a/challenge-148/dave-jacoby/perl/ch-2.pl b/challenge-148/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..330ce7e8d0
--- /dev/null
+++ b/challenge-148/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures state };
+no warnings qw{ experimental };
+
+use Algorithm::Permute;
+
+my @triplets;
+
+my $i = 0;
+while ( scalar @triplets < 5 ) {
+ for my $j ( 1 .. $i ) {
+ for my $k ( 1 .. $j ) {
+ my $p = Algorithm::Permute->new( [ $i, $j, $k ] );
+ while ( my @res = $p->next ) {
+ my $t = test_cardano(@res);
+ if ( $t == 1 ) {
+ push @triplets, \@res;
+ }
+ }
+ }
+ }
+ $i++;
+ last if $i > 1000;
+}
+
+for my $ct (@triplets) {
+ my ( $a, $b, $c ) = $ct->@*;
+ print <<"END";
+ A: $a\tB: $b\tC: $c
+END
+}
+
+sub test_cardano ( $a, $b, $c ) {
+ my $sqrtc = sqrt $c;
+
+ # not necessary for the first five
+ if ( $a > $b * $sqrtc ) {
+ return cuberoot( $a + $b * $sqrtc ) + cuberoot( $a - $b * $sqrtc );
+ }
+
+ return cuberoot( $a + $b * $sqrtc ) +
+ -1 * cuberoot( abs( $a - $b * $sqrtc ) );
+}
+
+sub cuberoot ($n ) { return $n**( 1 / 3 ) }