aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-01-20 09:28:24 +0000
committerGitHub <noreply@github.com>2022-01-20 09:28:24 +0000
commit7bd7c5e68c3890597a68dfd057746a9ecaaedcaa (patch)
tree3c6970ce027d630f100a09a2df730b2ec7292628
parent76d0a7ea2f26260d6bc769e796368ca85eac5733 (diff)
parenta537da1ec8739a583966332e05d29bc3acd0450a (diff)
downloadperlweeklychallenge-club-7bd7c5e68c3890597a68dfd057746a9ecaaedcaa.tar.gz
perlweeklychallenge-club-7bd7c5e68c3890597a68dfd057746a9ecaaedcaa.tar.bz2
perlweeklychallenge-club-7bd7c5e68c3890597a68dfd057746a9ecaaedcaa.zip
Merge pull request #5546 from mattneleigh/pwc148
new file: challenge-148/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-148/mattneleigh/perl/ch-1.pl44
-rwxr-xr-xchallenge-148/mattneleigh/perl/ch-2.pl130
2 files changed, 174 insertions, 0 deletions
diff --git a/challenge-148/mattneleigh/perl/ch-1.pl b/challenge-148/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..d3bea6ecc2
--- /dev/null
+++ b/challenge-148/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+# Tens-place and ones-place numbers that do
+# not have an 'e' in them; numbers from
+# sixty six to one hundred all have an 'e'
+# so just use a limited subset of digits
+my @tens = ( 0, 3, 4, 5, 6 );
+my @ones = ( 0, 2, 4, 6 );
+my @ebans;
+my $tens_digit;
+my $ones_digit;
+
+# Loop over the tens place digits
+foreach $tens_digit (@tens){
+ # Loop over the ones place digits
+ foreach $ones_digit (@ones){
+ if($tens_digit){
+ # Tens digit is not zero...
+ push(@ebans, $tens_digit . $ones_digit);
+ } else{
+ # Tens digit is zero...
+ push(@ebans, $ones_digit) if($ones_digit);
+ }
+ }
+}
+
+print("\nThe Eban numbers below 100 are:\n");
+print(join(", ", @ebans), "\n\n");
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
diff --git a/challenge-148/mattneleigh/perl/ch-2.pl b/challenge-148/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..36baaa45bd
--- /dev/null
+++ b/challenge-148/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,130 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my $count = 5;
+my @cardanos;
+
+@cardanos = calculate_first_cardano_triplets($count);
+
+printf("\nThe first %d Cardano Triplets are:\n", scalar(@cardanos));
+foreach(@cardanos){
+ printf(" (%s)\n", join(", ", @{$_}));
+}
+print("\n");
+
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Find a specified number of the first Cardano Triplets, numbers (A, B, C) that
+# meet specified parameters (see link below)
+# Takes two arguments:
+# * The number of Triplets to find; if this number is zero (0), ALL Cardano
+# Triplets whose members' sum is less than or equal to the maximum sum value
+# (see below) will be found; if there aren't that many triplets within the
+# search range, the returned list (see farther below) will be shorter than
+# specified but will contain all Triplets within the range
+# * The maximum sum of A, B, and C up to which we'll search for Triplets; if
+# this argument is omitted or is false a default of 1000 will be used
+# Returns:
+# * A list of refs to the Cardano Triplets found, e.g.:
+# (
+# [ 2, 1, 5 ],
+# [ 5, 1, 52 ],
+# [ 5, 2, 13 ],
+# ...
+# )
+# NOTES: Cardano Triplets are described here:
+# https://projecteuler.net/problem=251
+# Which particular triplets you get will depend on the value of the maximum
+# sum.
+################################################################################
+sub calculate_first_cardano_triplets{
+ my $count = int(shift());
+ my $max = shift();
+
+ my @cardanos = ();
+ my $a;
+ my $b;
+ my $c;
+
+ if(defined($max) && $max){
+ $max = int($max);
+ } else{
+ $max = 1000;
+ }
+
+ # Traditionally this is done with the
+ # variables adding up to some
+ # particular maximum value...
+ for($a=1; $a<=($max - 2); $a++){
+ for($b=1; $b<=($max - $a - 1); $b++){
+ for($c=1; $c<=($max - $a - $b); $c++){
+ my $b_sqrt_c = $b * sqrt($c);
+
+ # The calculated value should be zero
+ # if we have a Cardano Triplet, but it
+ # could be slightly off because of
+ # round-off error
+ if(
+ abs(cbrt($a + $b_sqrt_c) + cbrt($a - $b_sqrt_c) - 1)
+ <
+ 0.000000001
+ ){
+ # We have a Cardano Triplet
+ push(@cardanos, [ $a, $b, $c ]);
+
+ # Break out of all loops if there's a
+ # count defined and we've hit it
+ $max = 0
+ if($count && (scalar(@cardanos) == $count));
+ }
+
+ }
+ }
+ }
+
+ return(@cardanos);
+
+}
+
+
+
+################################################################################
+# Compute the cube root of a number
+# Takes one argument:
+# * The number whose cube root is desired
+# Returns:
+# * The cube root
+################################################################################
+sub cbrt{
+ my $x = shift();
+
+ if($x < 0){
+
+ # $x is negative; play some sign games
+ return(-((-$x) ** (1 / 3)));
+
+ } else{
+
+ # $x is positive; very straightforward
+ return($x ** (1 / 3));
+
+ }
+
+}
+
+
+