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
13 changes: 8 additions & 5 deletions src/algorithms/primes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
*
* @param n Number to check
* @return True if n is prime, false otherwise
*/
*/
bool
Primes::IsPrime(int n) {
if (n <= 1) {
if (n <= 3) {
return n > 1;
}
if (n % 2 == 0 || n % 3 == 0) {
return false;
}
for (int i = 2; i < n; i += 1) {
if (n % i == 0) {
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 +54,4 @@ Primes::PrimeFactors(int n) {
}
}
return factors;
}
}
31 changes: 14 additions & 17 deletions src/control/double.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <unordered_map>
#include "double.h"

/**
Expand All @@ -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;
}
Expand All @@ -44,22 +43,20 @@ 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;
}

/**
Expand Down Expand Up @@ -98,4 +95,4 @@ DoubleForLoop::SumMatrix(std::vector<std::vector<int>> matrix) {
}
}
return sum;
}
}
35 changes: 6 additions & 29 deletions src/control/single.cc
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -41,24 +30,12 @@ SingleForLoop::MaxVector(std::vector<int> &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;
}
}
13 changes: 6 additions & 7 deletions src/datastructures/list.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <algorithm>
#include "list.h"

/** @brief shuffles a list into a new list
Expand Down Expand Up @@ -28,14 +29,12 @@ 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);
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;
}
}
20 changes: 3 additions & 17 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 @@ -72,25 +73,10 @@ OpsVector::SearchVector(std::vector<int> &v, int n) {
return ret;
}

/**
* @brief Sorts the vector in ascending order.
*
* @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;
}
}
}
std::sort(ret.begin(), ret.end());
return ret;
}

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

return ret;
}
}