From 24733fad6c9fb0b6b40b1b6ac0e3da85890fae9c Mon Sep 17 00:00:00 2001 From: TurinTech Bot Date: Wed, 7 Feb 2024 12:11:43 +0000 Subject: [PATCH] Artemis Changes --- src/algorithms/primes.cc | 20 ++++++++++++++++---- src/algorithms/sort.cc | 2 +- src/control/double.cc | 15 ++++++--------- src/control/single.cc | 10 +++------- src/datastructures/linkedlist.cc | 10 ++++------ src/datastructures/vector.cc | 6 +++--- src/generator/genvector.cc | 7 ++----- 7 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/algorithms/primes.cc b/src/algorithms/primes.cc index d4250ab..26c44ea 100644 --- a/src/algorithms/primes.cc +++ b/src/algorithms/primes.cc @@ -5,12 +5,24 @@ * @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) { + for (int i = 2; i < n; i++) { + if (n % i == 0) { + return false; + } + } + return true; +} + if (n % i == 0) { + return false; + } + } + return true; +} if (n % i == 0) { return false; } @@ -51,4 +63,4 @@ Primes::PrimeFactors(int n) { } } return factors; -} +} \ No newline at end of file diff --git a/src/algorithms/sort.cc b/src/algorithms/sort.cc index 9df61bb..e8e45f2 100644 --- a/src/algorithms/sort.cc +++ b/src/algorithms/sort.cc @@ -57,4 +57,4 @@ Sort::MaxN(std::vector &v, int n) { } return ret; -} +} \ No newline at end of file diff --git a/src/control/double.cc b/src/control/double.cc index bb9dba1..d853c8c 100644 --- a/src/control/double.cc +++ b/src/control/double.cc @@ -29,9 +29,7 @@ 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; - } + sum += (i - 1) * i / 2; } return sum; } @@ -72,11 +70,10 @@ DoubleForLoop::CountPairs(std::vector v) { 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 minSize = std::min(v0.size(), v1.size()); + for (int i = 0; i < minSize; i += 1) { + if (v0[i] == v1[i]) { + count += 1; } } return count; @@ -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..afc9d52 100644 --- a/src/control/single.cc +++ b/src/control/single.cc @@ -50,15 +50,11 @@ SingleForLoop::MaxVector(std::vector &arr) { */ 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]; + if (i % m == 0) { + sum += i; } } return sum; -} +} \ No newline at end of file diff --git a/src/datastructures/linkedlist.cc b/src/datastructures/linkedlist.cc index 6eaa518..0026f27 100644 --- a/src/datastructures/linkedlist.cc +++ b/src/datastructures/linkedlist.cc @@ -79,12 +79,10 @@ LinkedList::AddNode(int n) { */ bool LinkedList::SearchList(int n) { - Node *current = head; - while (current != nullptr) { + for (Node *current = head; current != nullptr; current = current->next) { if (current->data == n) { return true; } - current = current->next; } return false; } @@ -92,8 +90,8 @@ LinkedList::SearchList(int n) { /** @brief Reverses the linked list * */ -void -LinkedList::ReverseList() { + + void LinkedList::ReverseList() { Node *current = head; Node *prev = nullptr; Node *next = nullptr; @@ -120,4 +118,4 @@ LinkedList::At(int n) { current = current->next; } return current->data; -} +} \ No newline at end of file diff --git a/src/datastructures/vector.cc b/src/datastructures/vector.cc index c275043..339c3b9 100644 --- a/src/datastructures/vector.cc +++ b/src/datastructures/vector.cc @@ -59,8 +59,8 @@ 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) { + for (int i = 0; i < (int) ret.size() - 1; i += 1) { + for (int j = 0; j < (int) ret.size() - i - 1; j += 1) { if (ret[j] > ret[j + 1]) { int temp = ret[j]; ret[j] = ret[j + 1]; @@ -126,4 +126,4 @@ OpsVector::MergeVectors(std::vector &v1, std::vector &v2) { } return ret; -} +} \ No newline at end of file diff --git a/src/generator/genvector.cc b/src/generator/genvector.cc index ecfe47b..c380f4f 100644 --- a/src/generator/genvector.cc +++ b/src/generator/genvector.cc @@ -10,13 +10,10 @@ */ std::vector GenVector::RandomVector(int n, int m) { - std::vector ret = std::vector(n); - - srand(0); + std::vector ret(n); for (int i = 0; i < n; i += 1) { ret[i] = rand() % m; } - return ret; } @@ -60,4 +57,4 @@ GenVector::RandomSquareMatrix(int n, int m) { } return ret; -} +} \ No newline at end of file