From 42533cf1aa600876009bf27d9f6f2b165d7c6149 Mon Sep 17 00:00:00 2001 From: TurinTech Bot Date: Tue, 16 Jul 2024 12:37:16 +0000 Subject: [PATCH] Artemis Changes --- src/algorithms/primes.cc | 20 +++++------ src/control/double.cc | 66 ++++++++++++------------------------ src/control/single.cc | 27 ++++----------- src/datastructures/list.cc | 10 +++--- src/datastructures/vector.cc | 19 +++-------- 5 files changed, 49 insertions(+), 93 deletions(-) diff --git a/src/algorithms/primes.cc b/src/algorithms/primes.cc index d4250ab..cfbaa55 100644 --- a/src/algorithms/primes.cc +++ b/src/algorithms/primes.cc @@ -1,17 +1,17 @@ #include "primes.h" -/** @brief Checks if a number is prime - * - * @param n Number to check - * @return True if n is prime, false otherwise - */ -bool -Primes::IsPrime(int n) { +bool Primes::IsPrime(int n) { if (n <= 1) { return false; } - for (int i = 2; i < n; i += 1) { - if (n % i == 0) { + if (n <= 3) { + return true; + } + if (n % 2 == 0 || n % 3 == 0) { + return false; + } + for (int i = 5; i * i <= n; i += 6) { + if (n % i == 0 || n % (i + 2) == 0) { return false; } } @@ -51,4 +51,4 @@ Primes::PrimeFactors(int n) { } } return factors; -} +} \ No newline at end of file diff --git a/src/control/double.cc b/src/control/double.cc index bb9dba1..3676304 100644 --- a/src/control/double.cc +++ b/src/control/double.cc @@ -1,3 +1,5 @@ +#include +#include #include "double.h" /** @@ -5,33 +7,20 @@ * * @param n * @return the sum of all values squared from 0 to n - */ + */ long DoubleForLoop::SumSquare(int n) { long sum = 0; for (int i = 0; i < n; i += 1) { - for (int j = 0; j < n; j += 1) { - if (i == j) { - sum = sum + (long) (i * j); - } - } + sum += (long) (i * i); } return sum; } -/** - * @brief Sums all triangle numbers from T(1) to T(n) - * - * @param n - * @return the sum of all triangle numbers from T(1) to T(n) - */ -long -DoubleForLoop::SumTriangle(int n) { +long DoubleForLoop::SumTriangle(int n) { long sum = 0; - for (int i = 0; i < n + 1; i += 1) { - for (int j = 0; j < i; j += 1) { - sum = sum + (long) j; - } + for (int i = 0; i <= n; i++) { + sum += (i * (i - 1)) / 2; } return sum; } @@ -44,39 +33,28 @@ DoubleForLoop::SumTriangle(int n) { * * @param v * @return the number of pairs in an vay - */ + */ int DoubleForLoop::CountPairs(std::vector v) { - int count = 0; + std::unordered_map counts; for (int i = 0; i < (int) v.size(); i += 1) { - int nDuplicates = 0; - for (int j = 0; j < (int) v.size(); j += 1) { - if (v[i] == v[j]) { - nDuplicates += 1; - } - } - if (nDuplicates == 2) { - count += 1; + counts[v[i]]++; + } + int nPairs = 0; + for (auto it = counts.begin(); it != counts.end(); ++it) { + if (it->second == 2) { + nPairs++; } } - return count / 2; + return nPairs; } -/** - * @brief Counts the number of instances where the values at the same index are equal - * - * @param v0 - * @param v1 - * @return the number of instances where the values at the same index are equal - */ -int -DoubleForLoop::CountDuplicates(std::vector v0, std::vector v1) { +int DoubleForLoop::CountDuplicates(std::vector v0, std::vector v1) { int count = 0; - for (int i = 0; i < (int) v0.size(); i += 1) { - for (int j = 0; j < (int) v1.size(); j += 1) { - if (i == j && v0[i] == v1[j]) { - count += 1; - } + int size = std::min(v0.size(), v1.size()); + for (int i = 0; i < size; i += 1) { + if (v0[i] == v1[i]) { + count += 1; } } return count; @@ -98,4 +76,4 @@ DoubleForLoop::SumMatrix(std::vector> matrix) { } } return sum; -} +} \ No newline at end of file diff --git a/src/control/single.cc b/src/control/single.cc index 06ad4a9..4b4fc1d 100644 --- a/src/control/single.cc +++ b/src/control/single.cc @@ -5,18 +5,11 @@ * * @param n the upper bound (non-inclusive) * @return the sum of all integer values from 0 to n - */ + */ int SingleForLoop::SumRange(int n) { - int array[n]; - int sum = 0; - for (int i = 0; i < n; i += 1) { - array[i] = i; - } - for (int i = 0; i < n; i += 1) { - sum += array[i]; - } - return sum; + // Use the formula for the sum of arithmetic sequence + return (n * (n - 1)) / 2; } /** @@ -47,18 +40,12 @@ SingleForLoop::MaxVector(std::vector &arr) { * @param n the upper bound (non-inclusive) * @param m the modulus * @return the sum of all values from 0 to n that are divisible by m - */ + */ int SingleForLoop::SumModulus(int n, int m) { - int array[n]; int sum = 0; - for (int i = 0; i < n; i += 1) { - array[i] = i; - } - for (int i = 0; i < n; i += 1) { - if (array[i] % m == 0) { - sum += array[i]; - } + for (int i = 0; i < n; i += m) { + sum += i; } return sum; -} +} \ No newline at end of file diff --git a/src/datastructures/list.cc b/src/datastructures/list.cc index 9adddee..f61e111 100644 --- a/src/datastructures/list.cc +++ b/src/datastructures/list.cc @@ -28,14 +28,14 @@ OpsList::Shuffle(std::list &l) { * @param start the start index of the slice * @param end the end index of the slice (exclusive) * @return a new list with the elements of l sliced - */ + */ std::list OpsList::Slice(std::list &l, int start, int end) { std::list ret; - for (int i = start; i < end; i++) { - std::list::iterator it = l.begin(); - std::advance(it, i); + std::list::iterator it = l.begin(); + std::advance(it, start); + for (int i = start; i < end; i++, ++it) { ret.push_back(*it); } return ret; -} +} \ No newline at end of file diff --git a/src/datastructures/vector.cc b/src/datastructures/vector.cc index 9f2c72d..44a72dc 100644 --- a/src/datastructures/vector.cc +++ b/src/datastructures/vector.cc @@ -1,3 +1,4 @@ +#include #include "vector.h" #include @@ -77,21 +78,11 @@ OpsVector::SearchVector(std::vector &v, int n) { * * @param v Vector to sort. * @return The sorted vector. - */ + */ std::vector OpsVector::SortVector(std::vector &v) { - std::vector ret(v); - - for (int i = 0; i < (int) ret.size(); i += 1) { - for (int j = 0; j < (int) ret.size() - 1; j += 1) { - if (ret[j] > ret[j + 1]) { - int temp = ret[j]; - ret[j] = ret[j + 1]; - ret[j + 1] = temp; - } - } - } - return ret; + std::sort(v.begin(), v.end()); + return v; } /** @@ -149,4 +140,4 @@ OpsVector::MergeVectors(std::vector &v1, std::vector &v2) { } return ret; -} +} \ No newline at end of file