From cf638c1ad15fad8e756291ea904de1547d286592 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 24 Jun 2021 19:37:00 +0100 Subject: Add bc solution to challenge 003 --- challenge-003/paulo-custodio/bc/ch-1.bc | 55 +++++++++++++++++++++++++++++++++ challenge-006/paulo-custodio/bc/ch-2.bc | 2 +- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 challenge-003/paulo-custodio/bc/ch-1.bc 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 -- cgit From 47c31b2fddb7e0695795d799aaf1cdbf82083a3f Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 24 Jun 2021 19:40:05 +0100 Subject: Add bc solution to challenge 007 --- challenge-007/paulo-custodio/bc/ch-1.bc | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 challenge-007/paulo-custodio/bc/ch-1.bc 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 -- cgit From dd2a41d26cfab5d551a88ff16d0b6062518d34e0 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 24 Jun 2021 19:43:49 +0100 Subject: Add bc solution to challenge 008 --- challenge-008/paulo-custodio/bc/ch-1.bc | 49 +++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-008/paulo-custodio/bc/ch-1.bc 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 -- cgit From 786579fc8f23db25a7c8de1b2d420cf357d25c7b Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Thu, 24 Jun 2021 19:55:43 +0100 Subject: Add bc solution to challenge 009 --- challenge-009/paulo-custodio/bc/ch-1.bc | 31 ++++++++++++++++++++++++++++ challenge-009/paulo-custodio/t/test-1.yaml | 25 ++++++++++++++++++++++ challenge-009/paulo-custodio/t/test-2.yaml | 9 ++++++++ challenge-009/paulo-custodio/test.pl | 33 +++--------------------------- 4 files changed, 68 insertions(+), 30 deletions(-) create mode 100644 challenge-009/paulo-custodio/bc/ch-1.bc create mode 100644 challenge-009/paulo-custodio/t/test-1.yaml create mode 100644 challenge-009/paulo-custodio/t/test-2.yaml 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"), < Date: Thu, 24 Jun 2021 20:07:39 +0100 Subject: Add bc solution to challenge 011 --- challenge-011/paulo-custodio/bc/ch-1.bc | 39 ++++++++++++++++++++++++ challenge-011/paulo-custodio/t/test-1.yaml | 5 ++++ challenge-011/paulo-custodio/t/test-2.yaml | 37 +++++++++++++++++++++++ challenge-011/paulo-custodio/test.pl | 48 ++---------------------------- 4 files changed, 84 insertions(+), 45 deletions(-) create mode 100644 challenge-011/paulo-custodio/bc/ch-1.bc create mode 100644 challenge-011/paulo-custodio/t/test-1.yaml create mode 100644 challenge-011/paulo-custodio/t/test-2.yaml 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"), <