From 5c8892bf63889d5921a37b57da25efefc8bae41e Mon Sep 17 00:00:00 2001 From: Sandipan Mohanty <s.mohanty@fz-juelich.de> Date: Sat, 6 May 2023 11:28:48 +0200 Subject: [PATCH] Add day1 solutions --- day1/solutions/CMakeLists.txt | 15 ++++++++++++ day1/solutions/check_prime.cc | 25 +++++++++++++++++++ day1/solutions/fibonacci.cc | 33 +++++++++++++++++++++++++ day1/solutions/gcd.cc | 45 ++++++++++++++++++++++++++++++++++ day1/solutions/midpt.cc | 16 ++++++++++++ day1/solutions/min_of_three.cc | 21 ++++++++++++++++ day1/solutions/mysqrt.cc | 32 ++++++++++++++++++++++++ day1/solutions/raw1.cc | 12 +++++++++ 8 files changed, 199 insertions(+) create mode 100644 day1/solutions/CMakeLists.txt create mode 100644 day1/solutions/check_prime.cc create mode 100644 day1/solutions/fibonacci.cc create mode 100644 day1/solutions/gcd.cc create mode 100644 day1/solutions/midpt.cc create mode 100644 day1/solutions/min_of_three.cc create mode 100644 day1/solutions/mysqrt.cc create mode 100644 day1/solutions/raw1.cc diff --git a/day1/solutions/CMakeLists.txt b/day1/solutions/CMakeLists.txt new file mode 100644 index 0000000..b2fb111 --- /dev/null +++ b/day1/solutions/CMakeLists.txt @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 3.16) +set(CMAKE_CXX_EXTENSIONS OFF) +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +project(cxx2023_d1_solutions CXX) + + +FILE (GLOB sources ./ *.cc) +foreach(source ${sources}) + get_filename_component(withoutext "${source}" NAME_WE) + add_executable("${withoutext}" "${source}") +endforeach() + + diff --git a/day1/solutions/check_prime.cc b/day1/solutions/check_prime.cc new file mode 100644 index 0000000..de7bfc2 --- /dev/null +++ b/day1/solutions/check_prime.cc @@ -0,0 +1,25 @@ +#include <iostream> + +auto is_prime(unsigned int n) -> bool +{ + bool found_divisor = false; + for (auto i = 2U; + (not found_divisor) and i * i <= n; + ++i) { + if (n % i == 0U) found_divisor = true; + } + return (not found_divisor); +} + +auto main() -> int +{ + std::cout << "Number to check: "; + unsigned int inp{}; + std::cin >> inp; + if (is_prime(inp)) + std::cout << "Yes, that's a prime.\n"; + else + std::cout << "Not a prime.\n"; +} + + diff --git a/day1/solutions/fibonacci.cc b/day1/solutions/fibonacci.cc new file mode 100644 index 0000000..307d6ac --- /dev/null +++ b/day1/solutions/fibonacci.cc @@ -0,0 +1,33 @@ +#include <iostream> +#include <limits> + +auto main() -> int +{ + size_t umax{ std::numeric_limits<size_t>::max() }; + unsigned nfib = 0; + size_t f0{ 0 }, f1{ 1 }; + do { + size_t f2 = f0 + f1; + f0 = f1; + f1 = f2; + ++nfib; + } while (f1 < umax - f0); + std::cout << "The largest number in the Fibonacci sequence under " << umax << " is " << f1 << "\n"; + std::cout << "Number of entries in the Fibonacci sequence under " << umax << " is " << nfib << "\n"; +} +#include <iostream> +#include <limits> +#include <format> + +auto main() -> int +{ + using namespace std; + size_t umax{ numeric_limits<size_t>::max() }; + unsigned nfib = 0; + size_t f0{ 0 }, f1{ 1 }; + // Insert your code here + + cout << format(R"(The largest number in the Fibonacci sequence under {0:} is {1:}, +and there are {2:} entries under {0:} in that sequence. +)" , umax, f1, nfib); +} diff --git a/day1/solutions/gcd.cc b/day1/solutions/gcd.cc new file mode 100644 index 0000000..153b3e4 --- /dev/null +++ b/day1/solutions/gcd.cc @@ -0,0 +1,45 @@ +#include <iostream> + +auto euclid_gcd(unsigned long smaller, unsigned long larger) -> unsigned long +{ + if (smaller > larger) + std::swap(smaller, larger); + while (smaller != 0) { + auto rem = larger % smaller; + larger = smaller; + smaller = rem; + } + return larger; +} + +auto main(int argc, char* argv[]) -> int +{ + if (argc != 3) { + std::cout << "Usage:\n" + << argv[0] << " number1 number2\n"; + return 1; + } + unsigned long n1 = std::stoul(argv[1]), n2 = std::stoul(argv[2]); + std::cout << euclid_gcd(n1, n2) << "\n"; +} +#include <iostream> + +auto euclid_gcd(unsigned long smaller, unsigned long larger) -> unsigned long +{ + if (smaller > larger) + std::swap(smaller, larger); + // Fill in your code to implement Euclid's algorithm here. + + return ? ? ? ; +} + +auto main(int argc, char* argv[]) -> int +{ + if (argc != 3) { + std::cout << "Usage:\n" + << argv[0] << " number1 number2\n"; + return 1; + } + unsigned long n1 = std::stoul(argv[1]), n2 = std::stoul(argv[2]); + std::cout << euclid_gcd(n1, n2) << "\n"; +} diff --git a/day1/solutions/midpt.cc b/day1/solutions/midpt.cc new file mode 100644 index 0000000..b5b420c --- /dev/null +++ b/day1/solutions/midpt.cc @@ -0,0 +1,16 @@ +// examples/midpt.cc +#include <iostream> + +auto mid(int a, int b) -> int +{ + return (a + b) / 2; +} +auto main() -> int +{ + int i = 0, j = 0; + std::cout << "Enter i, j: "; + std::cin >> i >> j; + std::cout << "A number half way between " << i << " and " << j + << " is " << mid(i, j) << "\n"; +} + diff --git a/day1/solutions/min_of_three.cc b/day1/solutions/min_of_three.cc new file mode 100644 index 0000000..7093015 --- /dev/null +++ b/day1/solutions/min_of_three.cc @@ -0,0 +1,21 @@ +// examples/min_of_three.cc +#include <iostream> + +auto min_of_three(int a, int b, int c) -> int +{ + if (a <= b) { + if (a <= c) return a; + else return c; + } else { + if (b <= c) return b; + else return c; + } +} +auto main() -> int +{ + int i = 0, j = 0, k = 0; + std::cout << "Enter i, j and k: "; + std::cin >> i >> j >> k; + std::cout << "The smallest of the three is " << min_of_three(i, j, k) << "\n"; +} + diff --git a/day1/solutions/mysqrt.cc b/day1/solutions/mysqrt.cc new file mode 100644 index 0000000..ae024aa --- /dev/null +++ b/day1/solutions/mysqrt.cc @@ -0,0 +1,32 @@ +#include <iostream> +#include <cmath> + +auto mysqrt(double x) -> double +{ + const auto eps = 1.0e-12; + const auto eps2 = eps * eps; + auto r0 = 0.5 * (1. + x); + auto r1 = x / r0; + while ((r0 - r1) * (r0 - r1) > eps2) { + r0 = 0.5 * (r0 + r1); + r1 = x / r0; + } + return r1; +} + +auto main() -> int +{ + double x{}; + std::cout << "Enter a positive real number: "; + std::cin >> x; + if (x > 0.) { + auto rm = mysqrt(x); + auto rs = std::sqrt(x); + std::cout << "Square root with own function = " << rm << "\n"; + std::cout << "Square root with standard function = " << rs << "\n"; + std::cout << "Difference = " << rm - rs << "\n"; + } else { + std::cout << "The input number needs to be positive.\n"; + } +} + diff --git a/day1/solutions/raw1.cc b/day1/solutions/raw1.cc new file mode 100644 index 0000000..abe1558 --- /dev/null +++ b/day1/solutions/raw1.cc @@ -0,0 +1,12 @@ +#include <iostream> +#include <string> + +auto main() -> int +{ + using namespace std; + cout << "Use a single backslash character at the end of the line to continue input to the next line, as shown ...\n" + << R"(OBJ=a.o \ + b.o\ + c.o +)"; +} -- GitLab