aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-06-24 21:41:26 +0100
committerGitHub <noreply@github.com>2021-06-24 21:41:26 +0100
commitc9d2138108edc544718e2fe95dc643f027cd62d4 (patch)
tree4bd9330e828797e60c8d2b287c9643734562e0da
parentcffacaf785da1db6527109548d555eb990071795 (diff)
parent3f3d45004b145f2b8bc15617e0cbce80142ccf5b (diff)
downloadperlweeklychallenge-club-c9d2138108edc544718e2fe95dc643f027cd62d4.tar.gz
perlweeklychallenge-club-c9d2138108edc544718e2fe95dc643f027cd62d4.tar.bz2
perlweeklychallenge-club-c9d2138108edc544718e2fe95dc643f027cd62d4.zip
Merge pull request #4337 from pauloscustodio/paulo-custodio
Paulo custodio
-rw-r--r--challenge-003/paulo-custodio/bc/ch-1.bc55
-rw-r--r--challenge-006/paulo-custodio/bc/ch-2.bc2
-rw-r--r--challenge-007/paulo-custodio/bc/ch-1.bc35
-rw-r--r--challenge-008/paulo-custodio/bc/ch-1.bc49
-rw-r--r--challenge-009/paulo-custodio/bc/ch-1.bc31
-rw-r--r--challenge-009/paulo-custodio/t/test-1.yaml25
-rw-r--r--challenge-009/paulo-custodio/t/test-2.yaml9
-rw-r--r--challenge-009/paulo-custodio/test.pl33
-rw-r--r--challenge-011/paulo-custodio/bc/ch-1.bc39
-rw-r--r--challenge-011/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-011/paulo-custodio/t/test-2.yaml37
-rw-r--r--challenge-011/paulo-custodio/test.pl48
12 files changed, 292 insertions, 76 deletions
diff --git a/challenge-003/paulo-custodio/bc/ch-1.bc b/challenge-003/paulo-custodio/bc/ch-1.bc
new file mode 100644
index 0000000000..119c7557f5
--- /dev/null
+++ b/challenge-003/paulo-custodio/bc/ch-1.bc
@@ -0,0 +1,55 @@
+#!/usr/bin/bc -ql
+
+/*
+Challenge 003
+
+Challenge #1
+Create a script to generate 5-smooth numbers, whose prime divisors are less or
+equal to 5. They are also called Hamming/Regular/Ugly numbers. For more
+information, please check this wikipedia.
+*/
+
+scale = 0
+
+num = read()
+
+define min(a,b) {
+ if (a < b) { return a; } else { return b; }
+}
+
+define min3(a,b,c) {
+ return min(a,min(b,c));
+}
+
+q2[0] = 1; q2size = 1
+q3[0] = 1; q3size = 1
+q5[0] = 1; q5size = 1
+
+for (i = 0; i < num; i++) {
+ /* next hamming: get smallest of the queue heads */
+ h = min3(q2[0], q3[0], q5[0])
+ h
+
+ /* shift used multiples */
+ if (h == q2[0]) {
+ for (j = 1; j < q2size; j++)
+ q2[j-1] = q2[j]
+ q2size = q2size-1
+ }
+ if (h == q3[0]) {
+ for (j = 1; j < q3size; j++)
+ q3[j-1] = q3[j]
+ q3size = q3size-1
+ }
+ if (h == q5[0]) {
+ for (j = 1; j < q5size; j++)
+ q5[j-1] = q5[j]
+ q5size = q5size-1
+ }
+
+ /* push next multiples */
+ q2[q2size] = 2*h; q2size = q2size+1
+ q3[q3size] = 3*h; q3size = q3size+1
+ q5[q5size] = 5*h; q5size = q5size+1
+}
+quit
diff --git a/challenge-006/paulo-custodio/bc/ch-2.bc b/challenge-006/paulo-custodio/bc/ch-2.bc
index 631718706d..3fa72be981 100644
--- a/challenge-006/paulo-custodio/bc/ch-2.bc
+++ b/challenge-006/paulo-custodio/bc/ch-2.bc
@@ -5,4 +5,4 @@ ex=pi*sqrt(163)
rk=e(ex)
scale=12
rk/1
-quit \ No newline at end of file
+quit
diff --git a/challenge-007/paulo-custodio/bc/ch-1.bc b/challenge-007/paulo-custodio/bc/ch-1.bc
new file mode 100644
index 0000000000..217322c92b
--- /dev/null
+++ b/challenge-007/paulo-custodio/bc/ch-1.bc
@@ -0,0 +1,35 @@
+#!/usr/bin/bc -ql
+
+/*
+Challenge 007
+
+Challenge #1
+Print all the niven numbers from 0 to 50 inclusive, each on their own line.
+A niven number is a non-negative number that is divisible by the sum of its
+digits.
+*/
+
+scale = 0
+
+num = read()
+
+define niven(n) {
+ auto rem, sum
+ sum = 0
+ rem = n
+ while (rem > 0) {
+ sum += rem % 10
+ rem /= 10
+ }
+ if ((n % sum) == 0) {
+ return 1
+ } else {
+ return 0
+ }
+}
+
+for (i = 1; i <= num; i++)
+ if (niven(i))
+ print i, "\n"
+
+quit
diff --git a/challenge-008/paulo-custodio/bc/ch-1.bc b/challenge-008/paulo-custodio/bc/ch-1.bc
new file mode 100644
index 0000000000..12053a2445
--- /dev/null
+++ b/challenge-008/paulo-custodio/bc/ch-1.bc
@@ -0,0 +1,49 @@
+#!/usr/bin/bc -ql
+
+/*
+Challenge 008
+
+Challenge #1
+Write a script that computes the first five perfect numbers. A perfect number
+is an integer that is the sum of its positive proper divisors (all divisors
+except itself). Please check Wiki for more information.
+This challenge was proposed by Laurent Rosenfeld.
+*/
+
+scale = 0
+
+num = read()
+
+define is_prime(n) {
+ auto i
+ if (n <= 1) return 0
+ if (n <= 3) return 1
+ if ((n % 2) == 0 || (n % 3) == 0) return 0
+ for (i = 5; i*i <= n; i += 6) {
+ if ((n % i) == 0 || (n % (i + 2)) == 0) return 0
+ }
+ return 1
+}
+
+define next_prime(n) {
+ if (n <= 1) return 2
+ n = n+1
+ while (!is_prime(n)) n = n+1
+ return n;
+}
+
+p = 1
+define next_perfect() {
+ auto f
+ while (1) {
+ p = next_prime(p)
+ f = (2 ^ p) - 1
+ if (is_prime(f)) return (2 ^ (p-1)) * f
+ }
+}
+
+for (i = 0; i < num; i++) {
+ print next_perfect(), "\n"
+}
+
+quit
diff --git a/challenge-009/paulo-custodio/bc/ch-1.bc b/challenge-009/paulo-custodio/bc/ch-1.bc
new file mode 100644
index 0000000000..0449047840
--- /dev/null
+++ b/challenge-009/paulo-custodio/bc/ch-1.bc
@@ -0,0 +1,31 @@
+#!/usr/bin/bc -ql
+
+/*
+Challenge 009
+
+Challenge #1
+Write a script that finds the first square number that has at least 5 distinct
+digits. This was proposed by Laurent Rosenfeld.
+*/
+
+scale = 0
+num = read()
+
+define num_diff_digits(n) {
+ auto digit, digits[], i, sum
+ while (n > 0) {
+ digit = n % 10
+ n /= 10
+ digits[digit] = 1
+ }
+ sum = 0
+ for (i = 0; i < 10; i++)
+ sum += digits[i]
+ return sum;
+}
+
+n = 1
+while (num_diff_digits(n*n) < num)
+ n = n+1
+n*n
+quit
diff --git a/challenge-009/paulo-custodio/t/test-1.yaml b/challenge-009/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..0e7966c514
--- /dev/null
+++ b/challenge-009/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,25 @@
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: 1
+- setup:
+ cleanup:
+ args: 2
+ input:
+ output: 16
+- setup:
+ cleanup:
+ args: 3
+ input:
+ output: 169
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: 1024
+- setup:
+ cleanup:
+ args: 5
+ input:
+ output: 12769
diff --git a/challenge-009/paulo-custodio/t/test-2.yaml b/challenge-009/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..06e3445fb4
--- /dev/null
+++ b/challenge-009/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,9 @@
+- setup:
+ cleanup:
+ args: 2 3 1 4 2 2 1 0
+ input:
+ output: |
+ |Data: 2, 3, 1, 4, 2, 2, 1, 0
+ |Standard ranking: 3, 2, 6, 1, 3, 3, 6, 8
+ |Modified ranking: 5, 2, 7, 1, 5, 5, 7, 8
+ |Dense ranking: 3, 2, 4, 1, 3, 3, 4, 5
diff --git a/challenge-009/paulo-custodio/test.pl b/challenge-009/paulo-custodio/test.pl
index c5275ed8f4..ba6c37260b 100644
--- a/challenge-009/paulo-custodio/test.pl
+++ b/challenge-009/paulo-custodio/test.pl
@@ -1,31 +1,4 @@
-#!/usr/bin/perl
-
-use strict;
-use warnings;
-use 5.030;
+#!/usr/bin/env perl
+use Modern::Perl;
use Test::More;
-
-is capture("perl perl/ch-1.pl 1"), "1\n";
-is capture("perl perl/ch-1.pl 2"), "16\n";
-is capture("perl perl/ch-1.pl 3"), "169\n";
-is capture("perl perl/ch-1.pl 4"), "1024\n";
-is capture("perl perl/ch-1.pl 5"), "12769\n";
-
-
-is capture("perl perl/ch-2.pl 2 3 1 4 2 2 1 0"), <<END;
-Data: 2, 3, 1, 4, 2, 2, 1, 0
-Standard ranking: 3, 2, 6, 1, 3, 3, 6, 8
-Modified ranking: 5, 2, 7, 1, 5, 5, 7, 8
-Dense ranking: 3, 2, 4, 1, 3, 3, 4, 5
-END
-
-
-done_testing;
-
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \r\t]*\n/\n/g;
- return $out;
-}
+require '../../challenge-001/paulo-custodio/test.pl';
diff --git a/challenge-011/paulo-custodio/bc/ch-1.bc b/challenge-011/paulo-custodio/bc/ch-1.bc
new file mode 100644
index 0000000000..c8e8ea3a1c
--- /dev/null
+++ b/challenge-011/paulo-custodio/bc/ch-1.bc
@@ -0,0 +1,39 @@
+#!/usr/bin/bc -ql
+
+/*
+Challenge 011
+
+Challenge #1
+Write a script that computes the equal point in the Fahrenheit and Celsius
+scales, knowing that the freezing point of water is 32 °F and 0 °C, and that
+the boiling point of water is 212 °F and 100 °C. This challenge was proposed
+by Laurent Rosenfeld.
+*/
+
+scale = 10
+
+/*
+F = (C * 9/5) + 32
+F = C = x
+ => f(x) = (x * 9/5) + 32 - x = 0
+<=> f(x) = (9/5 - 1) * x + 32
+ f'(x) = 9/5 - 1
+*/
+
+define f(x) { return (9/5 - 1) * x + 32 }
+define df(x) { return 9/5 - 1 }
+
+define newton(x0) {
+ auto x1, tmp
+ x1 = x0+1
+ while (x0 != x1) {
+ x1 = x0 - f(x0)/df(x0)
+ tmp = x1; x1 = x0; x0 = tmp
+ }
+ return x0
+}
+
+t = newton(0)
+scale = 0
+t/1
+quit
diff --git a/challenge-011/paulo-custodio/t/test-1.yaml b/challenge-011/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..5ea8bd975e
--- /dev/null
+++ b/challenge-011/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: -40
diff --git a/challenge-011/paulo-custodio/t/test-2.yaml b/challenge-011/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..3d3d7b7bb3
--- /dev/null
+++ b/challenge-011/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,37 @@
+- setup:
+ cleanup:
+ args: 1
+ input:
+ output: |
+ |[[1]]
+- setup:
+ cleanup:
+ args: 2
+ input:
+ output: |
+ |[[1, 0]
+ | [0, 1]]
+- setup:
+ cleanup:
+ args: 4
+ input:
+ output: |
+ |[[1, 0, 0, 0]
+ | [0, 1, 0, 0]
+ | [0, 0, 1, 0]
+ | [0, 0, 0, 1]]
+- setup:
+ cleanup:
+ args: 10
+ input:
+ output: |
+ |[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
+ | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
+ | [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
+ | [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
+ | [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
+ | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
+ | [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
+ | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
+ | [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
+ | [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
diff --git a/challenge-011/paulo-custodio/test.pl b/challenge-011/paulo-custodio/test.pl
index 68b96b8cf7..ba6c37260b 100644
--- a/challenge-011/paulo-custodio/test.pl
+++ b/challenge-011/paulo-custodio/test.pl
@@ -1,46 +1,4 @@
-#!/usr/bin/perl
-
-use strict;
-use warnings;
-use 5.030;
+#!/usr/bin/env perl
+use Modern::Perl;
use Test::More;
-
-is capture("perl perl/ch-1.pl"), "-40\n";
-
-is capture("perl perl/ch-2.pl 1"), <<END;
-[[1]]
-END
-
-is capture("perl perl/ch-2.pl 2"), <<END;
-[[1, 0]
- [0, 1]]
-END
-
-is capture("perl perl/ch-2.pl 4"), <<END;
-[[1, 0, 0, 0]
- [0, 1, 0, 0]
- [0, 0, 1, 0]
- [0, 0, 0, 1]]
-END
-
-is capture("perl perl/ch-2.pl 10"), <<END;
-[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
- [0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
- [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
- [0, 0, 0, 1, 0, 0, 0, 0, 0, 0]
- [0, 0, 0, 0, 1, 0, 0, 0, 0, 0]
- [0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
- [0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
- [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]
- [0, 0, 0, 0, 0, 0, 0, 0, 1, 0]
- [0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
-END
-
-done_testing;
-
-sub capture {
- my($cmd) = @_;
- my $out = `$cmd`;
- $out =~ s/[ \r\t]*\n/\n/g;
- return $out;
-}
+require '../../challenge-001/paulo-custodio/test.pl';