Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/algorithms/primes.cc
Original file line number Diff line number Diff line change
@@ -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;
}
}
Expand Down Expand Up @@ -51,4 +51,4 @@ Primes::PrimeFactors(int n) {
}
}
return factors;
}
}
66 changes: 22 additions & 44 deletions src/control/double.cc
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
#include <algorithm>
#include <unordered_map>
#include "double.h"

/**
* @brief Sums all values squared from 0 to n
*
* @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;
}
Expand All @@ -44,39 +33,28 @@ DoubleForLoop::SumTriangle(int n) {
*
* @param v
* @return the number of pairs in an vay
*/
*/
int
DoubleForLoop::CountPairs(std::vector<int> v) {
int count = 0;
std::unordered_map<int, int> 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<int> v0, std::vector<int> v1) {
int DoubleForLoop::CountDuplicates(std::vector<int> v0, std::vector<int> 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;
Expand All @@ -98,4 +76,4 @@ DoubleForLoop::SumMatrix(std::vector<std::vector<int>> matrix) {
}
}
return sum;
}
}
27 changes: 7 additions & 20 deletions src/control/single.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -47,18 +40,12 @@ SingleForLoop::MaxVector(std::vector<int> &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;
}
}
10 changes: 5 additions & 5 deletions src/datastructures/list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ OpsList::Shuffle(std::list<int> &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<int>
OpsList::Slice(std::list<int> &l, int start, int end) {
std::list<int> ret;
for (int i = start; i < end; i++) {
std::list<int>::iterator it = l.begin();
std::advance(it, i);
std::list<int>::iterator it = l.begin();
std::advance(it, start);
for (int i = start; i < end; i++, ++it) {
ret.push_back(*it);
}
return ret;
}
}
19 changes: 5 additions & 14 deletions src/datastructures/vector.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include "vector.h"

#include <iostream>
Expand Down Expand Up @@ -77,21 +78,11 @@ OpsVector::SearchVector(std::vector<int> &v, int n) {
*
* @param v Vector to sort.
* @return The sorted vector.
*/
*/
std::vector<int>
OpsVector::SortVector(std::vector<int> &v) {
std::vector<int> 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;
}

/**
Expand Down Expand Up @@ -149,4 +140,4 @@ OpsVector::MergeVectors(std::vector<int> &v1, std::vector<int> &v2) {
}

return ret;
}
}