Skip to content

Commit

Permalink
Starting CPP implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Fazendaaa committed Nov 17, 2024
1 parent bd150cd commit bfa1049
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 16 deletions.
4 changes: 4 additions & 0 deletions src/r/R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ largestFactorial <- function(element) {
.Call('_projectEuler_largestFactorial', PACKAGE = 'projectEuler', element)
}

problem23Cpp <- function() {
.Call('_projectEuler_problem23Cpp', PACKAGE = 'projectEuler')
}

#' Max Palindrome
#'
#' @description
Expand Down
22 changes: 15 additions & 7 deletions src/r/R/problem-23.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,25 @@ problem23 <- function() {
outerIndex <- 1
twoNumbers <- list()
result <- list()
sumOfResults <- 0

while (outerIndex < total) {
while (outerIndex <= total) {
current <- abundant[outerIndex]
#sequence <- c()
#sequence <- c(current + current, Map(function(item) current + item, tail(abundant, n = -index)))
#twoNumbers <- c(twoNumbers, Filter(function(item) item < limit, sequence))

innerIndex <- outerIndex

while (innerIndex < total) {
while (innerIndex <= total) {
sumOfTwo <- current + abundant[innerIndex]

if (sumOfTwo < limit) {
twoNumbers[sumOfTwo] <- sumOfTwo
} else {
if (sumOfTwo > limit) {
break
}

twoNumbers[sumOfTwo] <- sumOfTwo

innerIndex <- innerIndex + 1
}

Expand All @@ -100,13 +101,20 @@ problem23 <- function() {
for (item in 1:limit) {
if (is.null(unlist(twoNumbers[item]))) {
result[item] <- item
sumOfResults <- sumOfResults + item
}
}

print('filter done')
print('two numbers')
twoNumbers <- unlist(Filter(Negate(is.null), twoNumbers))
print(head(twoNumbers, n = 30))
print(tail(twoNumbers, n = 30))
result <- unlist(result)
print('result')
print(head(result, n = 30))
return (sum(result))
print(tail(result, n = 30))

return (sumOfResults)
#return (parallelReduce(lessThanLimit, function(acc, cur) acc + cur, 0))
}
}
15 changes: 8 additions & 7 deletions src/r/R/projectEuler.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,22 @@ enumerate <- function(...) {
#'
allDivisors <- function(element) {
index <- 2
divisors <- c(1)
limit <- element / 2
divisors <- list()
limit <- floor(element / 2)
increment <- if (0 == element %% 2) 1 else 2

divisors[1] <- 1
divisors[element] <- element

while(index <= limit) {
if (0 == element %% index) {
divisors <- c(divisors, index)
divisors[index] <- index
}

index <- index + increment
}

divisors <- c(divisors, element)

return (divisors)
return (unlist(Filter(Negate(is.null), divisors)))
}

#'
Expand Down Expand Up @@ -255,4 +256,4 @@ parallelReduce <- function(items, reducer, init) {
}

return (Reduce(reducer, parallelizeData(items, toParallel), init))
}
}
11 changes: 11 additions & 0 deletions src/r/src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// problem23Cpp
double problem23Cpp();
RcppExport SEXP _projectEuler_problem23Cpp() {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
rcpp_result_gen = Rcpp::wrap(problem23Cpp());
return rcpp_result_gen;
END_RCPP
}
// maxPalindrome
double maxPalindrome(const double old, const double from, const double limit);
RcppExport SEXP _projectEuler_maxPalindrome(SEXP oldSEXP, SEXP fromSEXP, SEXP limitSEXP) {
Expand Down Expand Up @@ -74,6 +84,7 @@ END_RCPP
static const R_CallMethodDef CallEntries[] = {
{"_projectEuler_evenFibonacci", (DL_FUNC) &_projectEuler_evenFibonacci, 3},
{"_projectEuler_largestFactorial", (DL_FUNC) &_projectEuler_largestFactorial, 1},
{"_projectEuler_problem23Cpp", (DL_FUNC) &_projectEuler_problem23Cpp, 0},
{"_projectEuler_maxPalindrome", (DL_FUNC) &_projectEuler_maxPalindrome, 3},
{"_projectEuler_greatestProduct", (DL_FUNC) &_projectEuler_greatestProduct, 2},
{"_projectEuler_erastosthenesSieve", (DL_FUNC) &_projectEuler_erastosthenesSieve, 1},
Expand Down
80 changes: 80 additions & 0 deletions src/r/src/problem-23.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <Rcpp.h>

using namespace Rcpp;

const std::list<int> getDivisors(int number) {
int limit = number/2;
std::list<int> divisors = {1};

for (int index = 2; index <= limit; index += 1) {
if (0 == number % index) {
divisors.push_back(index);
}
}

divisors.push_back(number);

return divisors;
}

const bool isAbundant(int number) {
int sum = 0;
std::list<int> elements = getDivisors(number);

elements.pop_back();

for (auto i : elements) {
sum += i;
}

return sum > number;
}

const std::list<int> getAbundantNumbers(int limit) {
std::list<int> abundantNumbers = {};

for (int index = 1; index <= limit; index += 1) {
if (isAbundant(index)) {
abundantNumbers.push_back(index);
}
}

return abundantNumbers;
}

// [[Rcpp::export]]
double problem23Cpp() {
int limit = 28123, sum, total, start = 1;
std::list<int> allAbundant = getAbundantNumbers(limit);
std::list<int> sumOfTwo = {};
auto outerValue = allAbundant.begin(), innerValue = allAbundant.begin();

for (int outerIndex = 0; outerIndex < allAbundant.size(); outerIndex += 1) {
std::advance(outerValue, outerIndex);

for (int innerIndex = outerIndex; innerIndex < allAbundant.size(); innerIndex += 1) {
std::advance(innerValue, outerIndex);

sum = *outerValue + *innerValue;

if (sum > limit) {
break;
}

sumOfTwo.push_back(sum);
}
}

sumOfTwo.sort();
sumOfTwo.unique();

for (auto element: sumOfTwo) {
for (int index = start; index < element; index++) {
total += index;
}

start = element + 1;
}

return total;
}
14 changes: 12 additions & 2 deletions src/r/tests/testthat/test.problem-23.R
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,23 @@ context('Testing problem 23')
#
# expect_equal(problem23(input), output)
#})
#
#test_that('Wanted example', {
# output <- 4179871
# result <- problem23()
#
# print('result')
# print(result)
#
# expect_equal(result, output)
#})

test_that('Wanted example', {
output <- 4179871
result <- problem23()
result <- problem23Cpp()

print('result')
print(result)

expect_equal(result, output)
})
})

0 comments on commit bfa1049

Please sign in to comment.