aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-12-20 16:41:30 +0000
committerPaulo Custodio <pauloscustodio@gmail.com>2021-12-20 16:41:30 +0000
commit82300f89acda50a186fa56a9e737cb3edf9a1030 (patch)
tree2969fc7cec3c3e29decda940147f5d15a557d572
parent1aa8ecccec917bbdee515fef036e8f84c47dae22 (diff)
downloadperlweeklychallenge-club-82300f89acda50a186fa56a9e737cb3edf9a1030.tar.gz
perlweeklychallenge-club-82300f89acda50a186fa56a9e737cb3edf9a1030.tar.bz2
perlweeklychallenge-club-82300f89acda50a186fa56a9e737cb3edf9a1030.zip
Add Perl and Python solutions to challenge 144
-rw-r--r--challenge-001/paulo-custodio/untabify.pl2
-rw-r--r--challenge-144/paulo-custodio/perl/ch-1.pl27
-rw-r--r--challenge-144/paulo-custodio/perl/ch-2.pl79
-rw-r--r--challenge-144/paulo-custodio/python/ch-1.py39
-rw-r--r--challenge-144/paulo-custodio/python/ch-2.py66
-rw-r--r--challenge-144/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-144/paulo-custodio/t/test-2.yaml15
7 files changed, 232 insertions, 1 deletions
diff --git a/challenge-001/paulo-custodio/untabify.pl b/challenge-001/paulo-custodio/untabify.pl
index dfdd1f1143..b52da1ff08 100644
--- a/challenge-001/paulo-custodio/untabify.pl
+++ b/challenge-001/paulo-custodio/untabify.pl
@@ -10,7 +10,7 @@ for my $dir (<challenge-*/paulo-custodio>) {
my $iter = path($dir)->iterator({recurse=>1});
while (defined(my $path = $iter->())) {
next unless $path->is_file;
- next unless -T $path;
+ next unless -T $path;
next if $path =~ /~$/; # temp files
my $ext = ""; $path->basename =~ /(\.\w+)$/ and $ext = $1;
next if $ext eq "" || $ext =~ /\.(exe|o|obj|ali|ads)$/; # binaries
diff --git a/challenge-144/paulo-custodio/perl/ch-1.pl b/challenge-144/paulo-custodio/perl/ch-1.pl
new file mode 100644
index 0000000000..0b3014813b
--- /dev/null
+++ b/challenge-144/paulo-custodio/perl/ch-1.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+
+# Challenge 144
+#
+# TASK #1 > Semiprime
+# Submitted by: Mohammad S Anwar
+# Write a script to generate all Semiprime number <= 100.
+#
+# For more information about Semiprime, please checkout the wikipedia page.
+#
+#
+# In mathematics, a semiprime is a natural number that is the product of
+# exactly two prime numbers. The two primes in the product may equal each
+# other, so the semiprimes include the squares of prime numbers.
+#
+#
+# Example
+# 10 is Semiprime as 10 = 2 x 5
+# 15 is Semiprime as 15 = 3 x 5
+
+use Modern::Perl;
+use ntheory 'semi_primes';
+
+use constant MAX_NUM => 100;
+
+my $n = shift||MAX_NUM;
+say join(", ", @{semi_primes($n)});
diff --git a/challenge-144/paulo-custodio/perl/ch-2.pl b/challenge-144/paulo-custodio/perl/ch-2.pl
new file mode 100644
index 0000000000..180042f82c
--- /dev/null
+++ b/challenge-144/paulo-custodio/perl/ch-2.pl
@@ -0,0 +1,79 @@
+#!/usr/bin/perl
+
+# Challenge 144
+#
+# TASK #2 > Ulam Sequence
+# Submitted by: Mohammad S Anwar
+# You are given two positive numbers, $u and $v.
+#
+# Write a script to generate Ulam Sequence having at least 10 Ulam numbers
+# where $u and $v are the first 2 Ulam numbers.
+#
+# For more information about Ulam Sequence, please checkout the website.
+#
+# The standard Ulam sequence (the (1, 2)-Ulam sequence) starts with U1 = 1 and
+# U2 = 2. Then for n > 2, Un is defined to be the smallest integer that is the
+# sum of two distinct earlier terms in exactly one way and larger than all
+# earlier terms.
+
+use Modern::Perl;
+use Math::Combinatorics;
+use List::Util 'sum';
+
+use constant NUM_TERMS => 10;
+
+sub next_ulam {
+ my(@terms) = @_;
+
+ # get all combinations of 2 items from previous terms
+ my @combin = combine(2, @terms);
+
+ # compute sum for all combinations
+ my %sums;
+ for (@combin) {
+ my @items = @$_;
+ my $n = sum(@items);
+ $sums{$n} ||= [];
+ push @{$sums{$n}}, \@items;
+ }
+
+ # choose smallest sum that has only one possible combination and is larger
+ # than previous ones
+ for my $n (sort {$a<=>$b} keys %sums) {
+ next unless $n > $terms[-1]; # item not larger than previous
+ my @items = @{$sums{$n}};
+ next if @items>1; # more than one sum
+ return $n;
+ }
+
+ die "next item not foundm terms=@terms";
+}
+
+sub ulam_iter {
+ my($u, $v) = @_;
+ my @terms;
+ return sub {
+ if (@terms==0) {
+ push @terms, $u;
+ return $u;
+ }
+ elsif (@terms==1) {
+ push @terms, $v;
+ return $v;
+ }
+ else {
+ my $n = next_ulam(@terms);
+ push @terms, $n;
+ return $n;
+ }
+ };
+}
+
+my($u, $v) = @ARGV;
+my $it = ulam_iter($u, $v);
+my @seq;
+for (1..NUM_TERMS) {
+ push @seq, $it->();
+}
+
+say join(", ", @seq);
diff --git a/challenge-144/paulo-custodio/python/ch-1.py b/challenge-144/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..6598deecc0
--- /dev/null
+++ b/challenge-144/paulo-custodio/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/python3
+
+# Challenge 144
+#
+# TASK #1 > Semiprime
+# Submitted by: Mohammad S Anwar
+# Write a script to generate all Semiprime number <= 100.
+#
+# For more information about Semiprime, please checkout the wikipedia page.
+#
+#
+# In mathematics, a semiprime is a natural number that is the product of
+# exactly two prime numbers. The two primes in the product may equal each
+# other, so the semiprimes include the squares of prime numbers.
+#
+#
+# Example
+# 10 is Semiprime as 10 = 2 x 5
+# 15 is Semiprime as 15 = 3 x 5
+
+import sys
+from primePy import primes
+
+MAX_NUM = 100
+
+# list of primes up to max/smallest_prime
+prime_nums = primes.upto(MAX_NUM/2)
+
+# set of all semiprimes
+semiprime_set = set()
+for i in range(len(prime_nums)):
+ for j in range(len(prime_nums)):
+ n = prime_nums[i]*prime_nums[j]
+ semiprime_set.add(n)
+
+# sort and filter <= MAX_NUM
+semiprimes = sorted(filter(lambda x:x <= MAX_NUM, list(semiprime_set)))
+
+print(*semiprimes, sep=", ")
diff --git a/challenge-144/paulo-custodio/python/ch-2.py b/challenge-144/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..42397b1f50
--- /dev/null
+++ b/challenge-144/paulo-custodio/python/ch-2.py
@@ -0,0 +1,66 @@
+#!/usr/bin/python3
+
+# Challenge 144
+#
+# TASK #2 > Ulam Sequence
+# Submitted by: Mohammad S Anwar
+# You are given two positive numbers, $u and $v.
+#
+# Write a script to generate Ulam Sequence having at least 10 Ulam numbers
+# where $u and $v are the first 2 Ulam numbers.
+#
+# For more information about Ulam Sequence, please checkout the website.
+#
+# The standard Ulam sequence (the (1, 2)-Ulam sequence) starts with U1 = 1 and
+# U2 = 2. Then for n > 2, Un is defined to be the smallest integer that is the
+# sum of two distinct earlier terms in exactly one way and larger than all
+# earlier terms.
+
+import sys
+from itertools import combinations
+
+NUM_TERMS = 10
+
+def ulam_seq(u, v):
+ def next_ulam(terms):
+ # get all combinations of 2 items from previous terms
+ # compute sum for all combinations
+ sums = {}
+ for items in combinations(terms, 2):
+ n = sum(items)
+ if n not in sums:
+ sums[n] = []
+ sums[n].append(items)
+
+ # choose smallest sum that has only one possible combination and
+ # is larger than previous ones
+ for n in sorted(sums):
+ if n > terms[-1]:
+ if len(sums[n])==1:
+ return n
+ print("no solution found for ", *terms)
+ sys.exit(1)
+
+ terms = []
+
+ # first two terms
+ terms.append(u)
+ yield u
+
+ terms.append(v)
+ yield v
+
+ # other terms
+ while True:
+ n = next_ulam(terms)
+ terms.append(n)
+ yield n
+
+u = int(sys.argv[1])
+v = int(sys.argv[2])
+seq = []
+for n in ulam_seq(u, v):
+ seq.append(n)
+ if len(seq) >= NUM_TERMS:
+ break
+print(*seq, sep=", ")
diff --git a/challenge-144/paulo-custodio/t/test-1.yaml b/challenge-144/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..a78b6e85cd
--- /dev/null
+++ b/challenge-144/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 100
+ input:
+ output: 4, 6, 9, 10, 14, 15, 21, 22, 25, 26, 33, 34, 35, 38, 39, 46, 49, 51, 55, 57, 58, 62, 65, 69, 74, 77, 82, 85, 86, 87, 91, 93, 94, 95
diff --git a/challenge-144/paulo-custodio/t/test-2.yaml b/challenge-144/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..dc065304a3
--- /dev/null
+++ b/challenge-144/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,15 @@
+- setup:
+ cleanup:
+ args: 1 2
+ input:
+ output: 1, 2, 3, 4, 6, 8, 11, 13, 16, 18
+- setup:
+ cleanup:
+ args: 2 3
+ input:
+ output: 2, 3, 5, 7, 8, 9, 13, 14, 18, 19
+- setup:
+ cleanup:
+ args: 2 5
+ input:
+ output: 2, 5, 7, 9, 11, 12, 13, 15, 19, 23