diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-20 18:00:10 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-06-20 18:00:10 +0100 |
| commit | abff22fe18e53d4292497b8e84bbeab0ddd54e78 (patch) | |
| tree | 33c538393afd5d753dc4b6ee41cb061d7a79683a /challenge-006 | |
| parent | 3fc1f69b23631ed842b9244914e6e23a7a765425 (diff) | |
| download | perlweeklychallenge-club-abff22fe18e53d4292497b8e84bbeab0ddd54e78.tar.gz perlweeklychallenge-club-abff22fe18e53d4292497b8e84bbeab0ddd54e78.tar.bz2 perlweeklychallenge-club-abff22fe18e53d4292497b8e84bbeab0ddd54e78.zip | |
Add C++ solution to challenge 006
Diffstat (limited to 'challenge-006')
| -rw-r--r-- | challenge-006/paulo-custodio/cpp/ch-1.cpp | 45 | ||||
| -rw-r--r-- | challenge-006/paulo-custodio/cpp/ch-2.cpp | 33 |
2 files changed, 78 insertions, 0 deletions
diff --git a/challenge-006/paulo-custodio/cpp/ch-1.cpp b/challenge-006/paulo-custodio/cpp/ch-1.cpp new file mode 100644 index 0000000000..314584f1f9 --- /dev/null +++ b/challenge-006/paulo-custodio/cpp/ch-1.cpp @@ -0,0 +1,45 @@ +/* +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 <iostream> +#include <sstream> +#include <string> +#include <vector> +using namespace std; + +vector<int> nums; + +void parse_nums(const string& str) { + istringstream iss(str); + string num_str; + while (getline(iss, num_str, ',')) { + int num = atoi(num_str.c_str()); + nums.push_back(num); + } +} + +int main(int argc, char* argv[]) { + if (argc != 2) return EXIT_FAILURE; + parse_nums(argv[1]); + for (size_t i = 0; i < nums.size(); i++) { + cout << nums[i]; + if (i + 2 < nums.size() && + nums[i + 1] == nums[i] + 1 && + nums[i + 2] == nums[i] + 2) { + size_t j = 0; + while (i + j < nums.size() && nums[i + j] == nums[i] + j) + j++; + i += j - 1; + cout << "-" << nums[i]; + } + if (i + 1 < nums.size()) + cout << ","; + } + cout << endl; +} diff --git a/challenge-006/paulo-custodio/cpp/ch-2.cpp b/challenge-006/paulo-custodio/cpp/ch-2.cpp new file mode 100644 index 0000000000..b408b41945 --- /dev/null +++ b/challenge-006/paulo-custodio/cpp/ch-2.cpp @@ -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 <iostream> +#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); +} |
