Skip to content
Snippets Groups Projects
Commit 5c8892bf authored by Sandipan Mohanty's avatar Sandipan Mohanty
Browse files

Add day1 solutions

parent e02c63ec
No related branches found
No related tags found
No related merge requests found
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()
#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";
}
#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);
}
#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";
}
// 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";
}
// 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";
}
#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";
}
}
#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
)";
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment