From d3f9c3b411275569d0340bc5e6a86b7e74138ac7 Mon Sep 17 00:00:00 2001 From: ikselven Date: Fri, 28 Apr 2017 00:46:20 +0200 Subject: [PATCH] faster R implementation of project euler problem 87 --- 2017-04-11/pikatech_euler87.R | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100755 2017-04-11/pikatech_euler87.R diff --git a/2017-04-11/pikatech_euler87.R b/2017-04-11/pikatech_euler87.R new file mode 100755 index 0000000..17b80e1 --- /dev/null +++ b/2017-04-11/pikatech_euler87.R @@ -0,0 +1,24 @@ +library(numbers) + +#### +# limit: Obere Grenze für die Summe der Primzahltripel +# +# Gibt die Anzahl der einmaligen Primzahltripel +# bis zu einer oberen Grenze zurück. +#### +euler87 <- 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])) +} + +system.time(print(euler87(5e7)))