aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-09 16:03:52 +0100
committerGitHub <noreply@github.com>2021-10-09 16:03:52 +0100
commit5a7d28221241da0409feb0a0c235e93feb16af61 (patch)
treed7e2e73b7e2d4f0df2b043ebe9d138d508a4e701
parent78ee99dc08a73dcf814dcf9a0da478c5ff7b245b (diff)
parentda32205b923a88237ff618e2219810769ad603aa (diff)
downloadperlweeklychallenge-club-5a7d28221241da0409feb0a0c235e93feb16af61.tar.gz
perlweeklychallenge-club-5a7d28221241da0409feb0a0c235e93feb16af61.tar.bz2
perlweeklychallenge-club-5a7d28221241da0409feb0a0c235e93feb16af61.zip
Merge pull request #4989 from simongreen-net/master
sgreen solution to challenge 133
-rw-r--r--challenge-132/sgreen/README.md3
-rw-r--r--challenge-133/sgreen/README.md4
-rw-r--r--challenge-133/sgreen/blog.txt1
-rwxr-xr-xchallenge-133/sgreen/perl/ch-1.pl22
-rwxr-xr-xchallenge-133/sgreen/perl/ch-2.pl53
5 files changed, 78 insertions, 5 deletions
diff --git a/challenge-132/sgreen/README.md b/challenge-132/sgreen/README.md
deleted file mode 100644
index 0022b09d87..0000000000
--- a/challenge-132/sgreen/README.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# The Weekly Challenge 131
-
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-131-1fl1)
diff --git a/challenge-133/sgreen/README.md b/challenge-133/sgreen/README.md
index 0022b09d87..298af3387e 100644
--- a/challenge-133/sgreen/README.md
+++ b/challenge-133/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 131
+# The Weekly Challenge 133
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-131-1fl1)
+Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-133-7gi)
diff --git a/challenge-133/sgreen/blog.txt b/challenge-133/sgreen/blog.txt
new file mode 100644
index 0000000000..130dbc3790
--- /dev/null
+++ b/challenge-133/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-133-7gi
diff --git a/challenge-133/sgreen/perl/ch-1.pl b/challenge-133/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..4033f28040
--- /dev/null
+++ b/challenge-133/sgreen/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub main {
+ my $N = shift;
+
+ # Sanity check
+ die "You must specify a positive integer\n" unless defined $N;
+ die "The value doesn't appear to be a positive integer\n" unless $N =~ /^[1-9][0-9]*$/;
+
+ # We do this the crude way
+ my $isqrt = 1;
+ $isqrt++ while ( $isqrt + 1 )**2 <= $N;
+
+ # Display the result
+ say $isqrt;
+}
+
+main(@ARGV);
diff --git a/challenge-133/sgreen/perl/ch-2.pl b/challenge-133/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..8aab074fe3
--- /dev/null
+++ b/challenge-133/sgreen/perl/ch-2.pl
@@ -0,0 +1,53 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+use List::Util 'sum';
+
+sub _digit_sum {
+ # Given a string of numbers, calculate the sum of the digits
+ my $string = shift;
+ return sum split //, $string;
+}
+
+sub _prime_factors_sum {
+ my $number = shift;
+ my %factors = ();
+ my $counter = 2;
+
+ # Get the primes that make up this number
+ while ( $number != 1 ) {
+ if ( $number % $counter == 0 ) {
+ $factors{$counter}++;
+ $number /= $counter;
+ }
+ else {
+ $counter++;
+ }
+ }
+
+ # A prime is not a composite number, and therefore cannot be a
+ # Smiths number. Returning -1 ensures the eqaulity is false
+ return -1 if scalar( keys(%factors) ) == 1 and ( values(%factors) )[0] == 1;
+
+ # Return the sum of they sum of digits in the prime multiplied by the power
+ # For example 265 = 5¹ * 53¹. The equation is 5 * 1 + (5 + 3) * 1
+ return sum( map { _digit_sum($_) * $factors{$_} } keys %factors );
+}
+
+sub main {
+ my @smiths = ();
+
+ my $number = 1;
+ while ( ++$number ) {
+ # Add to the array if this is a Smiths number
+ push @smiths, $number if _digit_sum($number) == _prime_factors_sum($number);
+ last if @smiths == 10;
+ }
+
+ say join ', ', @smiths;
+}
+
+main();