diff --git a/src/control/double.cc b/src/control/double.cc index bb9dba1..565277d 100644 --- a/src/control/double.cc +++ b/src/control/double.cc @@ -1,3 +1,4 @@ +#include #include "double.h" /** @@ -5,33 +6,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,22 +32,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 +84,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/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