From b13d9fd45364cfe884ea8a7b5dd304b4e7d80087 Mon Sep 17 00:00:00 2001 From: pikatech Date: Wed, 10 May 2017 18:47:45 +0200 Subject: [PATCH] Add files via upload Two solutions. One simple and one faster. --- 87/euler87.R | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 87/euler87.R diff --git a/87/euler87.R b/87/euler87.R new file mode 100644 index 0000000..d10153d --- /dev/null +++ b/87/euler87.R @@ -0,0 +1,48 @@ +library(numbers) +library(microbenchmark) + +#### +# limit: Obere Grenze für die Summe der Primzahltripel +# +# Gibt die Anzahl der einmaligen Primzahltripel +# bis zu einer oberen Grenze zurück. +#### +simple87 <- function(limit) { + primes <- Primes(1, ceiling(limit^(1/2))) + + x2 <- primes^2 + x3 <- primes^3 + x4 <- primes^4 + x2 <- x2[x2 < limit] + x3 <- x3[x3 < limit] + x4 <- x4[x4 < limit] + + x <- expand.grid(x2, x3, x4) + xSums <- rowSums(x) + length(unique(xSums[xSums < limit])) +} + +# Wir erzeugen die Kombinationen selbst und +# generieren eine Matrix über cbind() +# Dadurch spart man sich die matrix <-> data.frame Konvertierung +faster87<-function(limit) { + primes <- Primes(1, ceiling(sqrt(limit))) + + x2 <- primes^2 + x3 <- primes^3 + x4 <- primes^4 + x2 <- x2[x2 < limit] + x3 <- x3[x3 < limit] + x4 <- x4[x4 < limit] + + x <- cbind(rep(x2, each=length(x3)*length(x4)), + rep(x3, times=length(x2)*length(x4)), + rep(x4, times=length(x2)*length(x3)) + ) + + xSums <- rowSums(x) + length(unique(xSums[xSums < limit])) +} + +ress <- microbenchmark(simple87(5e7), faster87(5e7), times=500L) +