aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-23 13:23:49 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-12-23 13:23:49 +0000
commit8bfe04ecd260f1c5294456eae74bcc582ded60df (patch)
tree76e851374b947b7ba0c81ed830dd21f7b7ce3252
parent204eb30b978d45b4d6b17adbf0ba69469cbbecb7 (diff)
parenta8bb8b3321e3967103e48cdbe331c49034fc5c04 (diff)
downloadperlweeklychallenge-club-8bfe04ecd260f1c5294456eae74bcc582ded60df.tar.gz
perlweeklychallenge-club-8bfe04ecd260f1c5294456eae74bcc582ded60df.tar.bz2
perlweeklychallenge-club-8bfe04ecd260f1c5294456eae74bcc582ded60df.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
-rw-r--r--challenge-144/sgreen/README.md5
-rwxr-xr-xchallenge-144/sgreen/perl/ch-1.pl48
-rwxr-xr-xchallenge-144/sgreen/perl/ch-2.pl42
-rwxr-xr-xchallenge-144/sgreen/python/ch-1.py36
-rwxr-xr-xchallenge-144/sgreen/python/ch-2.py37
5 files changed, 166 insertions, 2 deletions
diff --git a/challenge-144/sgreen/README.md b/challenge-144/sgreen/README.md
index 70c73fd01a..85b71b378f 100644
--- a/challenge-144/sgreen/README.md
+++ b/challenge-144/sgreen/README.md
@@ -1,3 +1,4 @@
-# The Weekly Challenge 143
+# The Weekly Challenge 144
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-143-36f6)
+Solution by Simon Green. Sorry, no blog this week. Hopefully the code
+is easy enough to understand.
diff --git a/challenge-144/sgreen/perl/ch-1.pl b/challenge-144/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..7248836658
--- /dev/null
+++ b/challenge-144/sgreen/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use List::Util 'none';
+
+sub get_primes {
+ my @primes = ();
+
+ # Return a list of all primes between 2 and 50 (being 100 ÷ 2)
+ I: foreach my $i ( 2 .. 50 ) {
+ foreach my $d ( 2 .. $i / 2 ) {
+ # If the number is divisable by something other than one and
+ # itself, it's not a prime
+ if ( $i % $d == 0 ) {
+ next I;
+ }
+ }
+
+ # It's a prime
+ push @primes, $i;
+ }
+
+ return @primes;
+}
+
+sub main {
+ my @primes = get_primes;
+ my @semiprimes = ();
+
+ I: foreach my $i (@primes) {
+ foreach my $j (@primes) {
+ my $x = $i * $j;
+
+ next I if $x > 100;
+
+ if ( none { $x == $_ } @semiprimes ) {
+ push @semiprimes, $x;
+ }
+ }
+ }
+
+ @semiprimes = sort { $a <=> $b } @semiprimes;
+ say join ', ', @semiprimes;
+}
+
+main();
diff --git a/challenge-144/sgreen/perl/ch-2.pl b/challenge-144/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..b1698c6eb8
--- /dev/null
+++ b/challenge-144/sgreen/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use POSIX 'ceil';
+use List::Util 'any';
+
+sub main {
+ my ( $m, $n ) = @_;
+
+ if ( $m < 1 ) {
+ raise ValueError('The first number must be a positive integer');
+ }
+ if ( $n < 1 ) {
+ raise ValueError('The second number must be a positive integer');
+ }
+
+ # We know the first three values in the sequence are the two numbers and the sum
+ my @seq = ( $m, $n, $m + $n );
+ my $current_value = $seq[-1] + 1;
+
+ while ( @seq < 10 ) {
+ my $sums = 0;
+ foreach my $i ( $m .. ceil( $current_value / 2 ) - 1 ) {
+ my $j = $current_value - $i;
+ if ( any { $i == $_ } @seq and any { $j == $_ } @seq ) {
+ $sums++;
+ }
+ }
+
+ if ( $sums == 1 ) {
+ push @seq, $current_value;
+ }
+
+ $current_value++;
+ }
+
+ say join ', ', @seq;
+}
+
+main(@ARGV);
diff --git a/challenge-144/sgreen/python/ch-1.py b/challenge-144/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..0301f27de9
--- /dev/null
+++ b/challenge-144/sgreen/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+def get_primes():
+ primes = []
+ # Return a list of all primes between 2 and 50 (being 100 ÷ 2)
+ for i in range(2, 51):
+ for d in range(2, int(i / 2) + 1):
+ # If the number is divisable by something other than one and
+ # itself, it's not a prime
+ if i % d == 0:
+ break
+ else:
+ # It's a prime
+ primes.append(i)
+
+ return primes
+
+
+def main():
+ primes = get_primes()
+ semiprimes = []
+
+ for i in primes:
+ for j in primes:
+ x = i * j
+ if x > 100:
+ break
+ if x not in semiprimes:
+ semiprimes.append(x)
+
+ semiprimes.sort()
+ print(*semiprimes, sep=', ')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-144/sgreen/python/ch-2.py b/challenge-144/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..086ad9a6f1
--- /dev/null
+++ b/challenge-144/sgreen/python/ch-2.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+
+import math
+import sys
+
+
+def main(inputs):
+ m = int(inputs[0])
+ n = int(inputs[1])
+
+ if m < 1:
+ raise ValueError('The first number must be a positive integer')
+ if n < 1:
+ raise ValueError('The second number must be a positive integer')
+
+ # We know the first three values in the sequence are the two numbers and
+ # the sum
+ seq = [m, n, m + n]
+ current_value = seq[-1] + 1
+
+ while len(seq) < 10:
+ sums = 0
+ for i in range(m, math.ceil(current_value / 2)):
+ j = current_value - i
+ if i in seq and j in seq:
+ sums += 1
+
+ if sums == 1:
+ seq.append(current_value)
+
+ current_value += 1
+
+ print(*seq, sep=', ')
+
+
+if __name__ == '__main__':
+ main(sys.argv[1:])