aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-06-20 01:34:18 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-06-20 01:34:18 +0100
commit0ed815ce97a445c5a43d6eb496b29970d1d89bc7 (patch)
tree0394c463a5501df01cbb25b5bbc2babe46c92146
parenta4d69a9944682f972182232cc6179a916f503a5b (diff)
downloadperlweeklychallenge-club-0ed815ce97a445c5a43d6eb496b29970d1d89bc7.tar.gz
perlweeklychallenge-club-0ed815ce97a445c5a43d6eb496b29970d1d89bc7.tar.bz2
perlweeklychallenge-club-0ed815ce97a445c5a43d6eb496b29970d1d89bc7.zip
Add C solution to challenge 006
-rw-r--r--challenge-006/paulo-custodio/c/ch-1.c55
-rw-r--r--challenge-006/paulo-custodio/c/ch-2.c33
-rw-r--r--challenge-006/paulo-custodio/perl/ch-1.pl4
-rw-r--r--challenge-006/paulo-custodio/perl/ch-2.pl10
-rw-r--r--challenge-006/paulo-custodio/t/test-1.yaml5
-rw-r--r--challenge-006/paulo-custodio/t/test-2.yaml5
6 files changed, 107 insertions, 5 deletions
diff --git a/challenge-006/paulo-custodio/c/ch-1.c b/challenge-006/paulo-custodio/c/ch-1.c
new file mode 100644
index 0000000000..64c296189e
--- /dev/null
+++ b/challenge-006/paulo-custodio/c/ch-1.c
@@ -0,0 +1,55 @@
+/*
+Challenge 006
+
+Challenge #1
+Create a script which takes a list of numbers from command line and print the
+same in the compact form. For example, if you pass "1,2,3,4,9,10,14,15,16"
+then it should print the compact form like "1-4,9,10,14-16".
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int* nums, nr_nums = 0;
+
+void* check_mem(void* p) {
+ if (!p) {
+ fputs("Out of memory", stderr);
+ exit(EXIT_FAILURE);
+ }
+ return p;
+}
+
+void parse_nums(char* str) {
+ char* num_str = strtok(str, ",");
+ while (num_str != NULL) {
+ int num = atoi(num_str);
+ nums = check_mem(realloc(nums, ++nr_nums * sizeof(int)));
+ nums[nr_nums - 1] = num;
+
+ num_str = strtok(NULL, ",");
+ }
+}
+
+int main(int argc, char* argv[]) {
+ if (argc != 2) return EXIT_FAILURE;
+ parse_nums(argv[1]);
+ for (int i = 0; i < nr_nums; i++) {
+ printf("%d", nums[i]);
+ if (i + 2 < nr_nums &&
+ nums[i + 1] == nums[i] + 1 &&
+ nums[i + 2] == nums[i] + 2) {
+ int j = 0;
+ while (i + j < nr_nums && nums[i + j] == nums[i] + j)
+ j++;
+ i += j - 1;
+ printf("-%d", nums[i]);
+ }
+ if (i + 1 < nr_nums)
+ printf(",");
+ }
+ printf("\n");
+ free(nums);
+}
+
diff --git a/challenge-006/paulo-custodio/c/ch-2.c b/challenge-006/paulo-custodio/c/ch-2.c
new file mode 100644
index 0000000000..668c1af192
--- /dev/null
+++ b/challenge-006/paulo-custodio/c/ch-2.c
@@ -0,0 +1,33 @@
+/*
+Challenge 006
+
+Challenge #2
+Create a script to calculate Ramanujan's constant with at least 32 digits of
+precision. Find out more about it here.
+
+The standard IEEE 754 double-precision binary floating-point format: binary64
+gives only 15 to 17 significant decimal digits
+*/
+
+#include <stdio.h>
+#include <gmp.h>
+#include <mpfr.h>
+
+int main() {
+ mpfr_set_default_prec(256); // 107 bits needed to represent 32 decimal digits
+
+ mpfr_t pi, e, k;
+ mpfr_inits(pi, e, k, NULL);
+
+ mpfr_const_pi(pi, MPFR_RNDN);
+
+ mpfr_set_str(e, "163", 10, MPFR_RNDN); // e = 163
+ mpfr_sqrt(e, e, MPFR_RNDN); // e = sqr(163)
+ mpfr_mul(e, pi, e, MPFR_RNDN); // e = pi*sqr(163)
+
+ mpfr_exp(k, e, MPFR_RNDN); // k = e^(pi*sqr(163))
+
+ mpfr_printf("%32.12Rf\n", k); // 18.12 = 32 precision
+
+ mpfr_clears(pi, e, k, NULL);
+}
diff --git a/challenge-006/paulo-custodio/perl/ch-1.pl b/challenge-006/paulo-custodio/perl/ch-1.pl
index 4b39e6774e..1e676808fa 100644
--- a/challenge-006/paulo-custodio/perl/ch-1.pl
+++ b/challenge-006/paulo-custodio/perl/ch-1.pl
@@ -3,7 +3,9 @@
# Challenge 006
#
# Challenge #1
-# Create a script which takes a list of numbers from command line and print the same in the compact form. For example, if you pass “1,2,3,4,9,10,14,15,16” then it should print the compact form like “1-4,9,10,14-16”.
+# Create a script which takes a list of numbers from command line and print the
+# same in the compact form. For example, if you pass "1,2,3,4,9,10,14,15,16"
+# then it should print the compact form like "1-4,9,10,14-16".
use strict;
use warnings;
diff --git a/challenge-006/paulo-custodio/perl/ch-2.pl b/challenge-006/paulo-custodio/perl/ch-2.pl
index 6159d42a0a..ac82ef2dae 100644
--- a/challenge-006/paulo-custodio/perl/ch-2.pl
+++ b/challenge-006/paulo-custodio/perl/ch-2.pl
@@ -3,9 +3,11 @@
# Challenge 006
#
# Challenge #2
-# Create a script to calculate Ramanujan’s constant with at least 32 digits of precision. Find out more about it here.
+# Create a script to calculate Ramanujan’s constant with at least 32 digits of
+# precision. Find out more about it here.
#
-# The standard IEEE 754 double-precision binary floating-point format: binary64 gives only 15 to 17 significant decimal digits
+# The standard IEEE 754 double-precision binary floating-point format: binary64
+# gives only 15 to 17 significant decimal digits
# Therefore must use the bignum library
use strict;
@@ -13,8 +15,8 @@ use warnings;
use 5.030;
use Math::BigFloat;
-my $accuracy = 64;
+my $accuracy = 128;
my $e = Math::BigFloat->bpi($accuracy) * Math::BigFloat->bsqrt(163, $accuracy);
my $k = Math::BigFloat->bexp($e, $accuracy);
-say $k;
+say $k->bround(30);
diff --git a/challenge-006/paulo-custodio/t/test-1.yaml b/challenge-006/paulo-custodio/t/test-1.yaml
new file mode 100644
index 0000000000..b307e93b28
--- /dev/null
+++ b/challenge-006/paulo-custodio/t/test-1.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args: 1,2,3,4,9,10,14,15,16
+ input:
+ output: 1-4,9,10,14-16
diff --git a/challenge-006/paulo-custodio/t/test-2.yaml b/challenge-006/paulo-custodio/t/test-2.yaml
new file mode 100644
index 0000000000..1c56582e97
--- /dev/null
+++ b/challenge-006/paulo-custodio/t/test-2.yaml
@@ -0,0 +1,5 @@
+- setup:
+ cleanup:
+ args:
+ input:
+ output: 262537412640768743.999999999999