From de74e710c8faabaa5a9892fd30b9971575de4c83 Mon Sep 17 00:00:00 2001 From: TurinTech Bot Date: Fri, 19 Jul 2024 09:05:29 +0000 Subject: [PATCH] Artemis Changes --- src/algorithms/primes.cc | 20 ++++++++++---------- src/control/double.cc | 31 ++++++++++++++----------------- src/control/single.cc | 20 ++++---------------- src/datastructures/list.cc | 13 ++++++------- src/datastructures/vector.cc | 20 ++++++-------------- 5 files changed, 40 insertions(+), 64 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..b323bc1 100644 --- a/src/control/double.cc +++ b/src/control/double.cc @@ -1,3 +1,4 @@ +#include #include "double.h" /** @@ -24,14 +25,12 @@ DoubleForLoop::SumSquare(int n) { * * @param n * @return the sum of all triangle numbers from T(1) to T(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 = 1; i <= n; ++i) { + sum += (long)i * (i - 1) / 2; } return sum; } @@ -44,22 +43,20 @@ 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; } /** @@ -98,4 +95,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..1288dca 100644 --- a/src/control/single.cc +++ b/src/control/single.cc @@ -41,24 +41,12 @@ SingleForLoop::MaxVector(std::vector &arr) { return max; } -/** - * @brief Sums all values from 0 to n that are divisible by m - * - * @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 SingleForLoop::SumModulus(int n, int m) { 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]; + if (i % m == 0) { + sum += i; } } return sum; -} +} \ No newline at end of file diff --git a/src/datastructures/list.cc b/src/datastructures/list.cc index 9adddee..af0c91d 100644 --- a/src/datastructures/list.cc +++ b/src/datastructures/list.cc @@ -1,3 +1,4 @@ +#include #include "list.h" /** @brief shuffles a list into a new list @@ -28,14 +29,12 @@ 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); - ret.push_back(*it); - } + auto it = std::next(l.begin(), start); + auto end_it = std::next(l.begin(), end); + ret.insert(ret.end(), it, end_it); return ret; -} +} \ No newline at end of file diff --git a/src/datastructures/vector.cc b/src/datastructures/vector.cc index 9f2c72d..f31dd79 100644 --- a/src/datastructures/vector.cc +++ b/src/datastructures/vector.cc @@ -1,3 +1,4 @@ +#include #include "vector.h" #include @@ -77,21 +78,12 @@ 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::vector ret(v); + std::sort(ret.begin(), ret.end()); + return ret; } /** @@ -149,4 +141,4 @@ OpsVector::MergeVectors(std::vector &v1, std::vector &v2) { } return ret; -} +} \ No newline at end of file