aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-171/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-171/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-171/paulo-custodio/python/ch-1.py54
-rw-r--r--challenge-171/paulo-custodio/python/ch-2.py32
4 files changed, 88 insertions, 2 deletions
diff --git a/challenge-171/paulo-custodio/perl/ch-1.pl b/challenge-171/paulo-custodio/perl/ch-1.pl
index b67f270336..735f7b2082 100644
--- a/challenge-171/paulo-custodio/perl/ch-1.pl
+++ b/challenge-171/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 171
#
diff --git a/challenge-171/paulo-custodio/perl/ch-2.pl b/challenge-171/paulo-custodio/perl/ch-2.pl
index 45c3da0abf..96387742bc 100644
--- a/challenge-171/paulo-custodio/perl/ch-2.pl
+++ b/challenge-171/paulo-custodio/perl/ch-2.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 171
#
diff --git a/challenge-171/paulo-custodio/python/ch-1.py b/challenge-171/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..a84a80698e
--- /dev/null
+++ b/challenge-171/paulo-custodio/python/ch-1.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+
+# Challenge 171
+#
+# Task 1: Abundant Number
+# Submitted by: Mohammad S Anwar
+#
+# Write a script to generate first 20 Abundant Odd Numbers.
+#
+# According to wikipedia,
+#
+# A number n for which the sum of divisors s(n) > 2n, or, equivalently, the
+# sum of proper divisors (or aliquot sum) s(n) > n.
+#
+#
+# For example, 945 is the first Abundant Odd Number.
+#
+# Sum of divisors:
+# 1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 105 + 135 + 189 + 315 = 975
+
+import sys
+import math
+from functools import reduce
+
+def divisors(n):
+ div_low = []
+ div_high = []
+ for i in range(1, int(math.sqrt(n)) + 1):
+ if n % i == 0:
+ div_low.append(i)
+ if n // i != i:
+ div_high.insert(0, n // i)
+ return div_low + div_high
+
+def proper_divisors(n):
+ div = divisors(n)
+ return div[:-1]
+
+def is_abundant(n):
+ return sum(proper_divisors(n)) > n
+
+def abundant_numbers(N):
+ abundant = []
+ n = 1
+ while len(abundant) < N:
+ if is_abundant(n):
+ abundant.append(n)
+ n += 1
+ return abundant
+
+if len(sys.argv) != 2:
+ raise ValueError("usage: script.py n")
+N = int(sys.argv[1])
+print(", ".join(map(str, abundant_numbers(N))))
diff --git a/challenge-171/paulo-custodio/python/ch-2.py b/challenge-171/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..f780acf173
--- /dev/null
+++ b/challenge-171/paulo-custodio/python/ch-2.py
@@ -0,0 +1,32 @@
+#!/usr/bin/env python3
+
+# Challenge 171
+#
+# Task 2: First-class Function
+# Submitted by: Mohammad S Anwar
+#
+# Create sub compose($f, $g) which takes in two parameters $f and $g as
+# subroutine refs and returns subroutine ref i.e. compose($f, $g)->($x) =
+# $f->($g->($x))
+#
+# e.g.
+#
+# $f = (one or more parameters function)
+# $g = (one or more parameters function)
+#
+# $h = compose($f, $g)
+# $f->($g->($x,$y, ..)) == $h->($x, $y, ..) for any $x, $y, ...
+
+import sys
+
+def compose(f, g):
+ return lambda x: f(g(x))
+
+def times3(x):
+ return 3 * x
+
+def times5(x):
+ return 5 * x
+
+h = compose(times3, times5)
+print(h(int(int(sys.argv[1]))))