From ecdf95c6b7682475f12d216e704ef7ae0bcf0773 Mon Sep 17 00:00:00 2001 From: TurinTech Bot Date: Wed, 24 Jul 2024 15:30:37 +0000 Subject: [PATCH] Artemis Changes --- src/algorithms/primes.cc | 12 +++++++++--- src/control/double.cc | 17 ++++------------- src/control/single.cc | 35 ++++++----------------------------- src/datastructures/vector.cc | 20 ++++++-------------- 4 files changed, 25 insertions(+), 59 deletions(-) diff --git a/src/algorithms/primes.cc b/src/algorithms/primes.cc index d4250ab..e9e2e04 100644 --- a/src/algorithms/primes.cc +++ b/src/algorithms/primes.cc @@ -4,13 +4,19 @@ * * @param n Number to check * @return True if n is prime, false otherwise - */ + */ bool Primes::IsPrime(int n) { if (n <= 1) { return false; } - for (int i = 2; i < n; i += 1) { + if (n == 2) { + return true; + } + if (n % 2 == 0) { + return false; + } + for (int i = 3; i * i <= n; i += 2) { if (n % i == 0) { return false; } @@ -51,4 +57,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..0d53154 100644 --- a/src/control/double.cc +++ b/src/control/double.cc @@ -19,19 +19,10 @@ DoubleForLoop::SumSquare(int n) { 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; } @@ -98,4 +89,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..3700dba 100644 --- a/src/control/single.cc +++ b/src/control/single.cc @@ -1,20 +1,9 @@ #include "single.h" -/** - * @brief Sums all integer values from 0 to n - * - * @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 SingleForLoop::SumRange(int 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]; + sum += i; } return sum; } @@ -41,24 +30,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/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