Merge branch 'master' of github.com:HackspaceJena/project-euler

This commit is contained in:
Lowl3v3l 2017-06-06 20:29:45 +02:00
commit 3872593d2f
6 changed files with 277 additions and 0 deletions

2
10/euler10.R Normal file
View file

@ -0,0 +1,2 @@
library(numbers)
sum(as.numeric(Primes(1L, 2000000L)))

48
10/euler10.cpp Normal file
View file

@ -0,0 +1,48 @@
// This solution needs the GNU MP Bignum library
// g++ euler10.cpp -o euler10 -lgmpxx -lgmp
#include <gmpxx.h>
#include <cmath>
#include <vector>
#include <iostream>
void sieve(std::vector<int>& primes, int const limit) {
if (limit < 2) {
return;
} else if (limit < 3) {
primes.push_back(2);
return;
}
primes.push_back(2);
primes.push_back(3);
for (int num = 5; num <= limit; num += 2) {
int sqrtnum = (int) std::sqrt(num);
bool isPrime = true;
for (auto primeIt = ++primes.begin(); primeIt != primes.end() && *primeIt <= sqrtnum; ++primeIt) {
if (num % *primeIt == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push_back(num);
}
}
}
int main(void) {
constexpr int limit = 2000000;
constexpr int PRIMELIMIT = limit - 1;
std::vector<int> primes;
sieve(primes, PRIMELIMIT);
mpz_class sum = 0;
for (auto primeIt = primes.begin(); primeIt != primes.end(); ++primeIt) {
sum += *primeIt;
}
std::cout << sum << std::endl;
return EXIT_SUCCESS;
}

26
16/euler16.cpp Normal file
View file

@ -0,0 +1,26 @@
// This solution needs the GNU MP Bignum library
// g++ euler16.cpp -o euler16 -lgmpxx -lgmp
#include <gmpxx.h>
#include <cmath>
#include <vector>
#include <iostream>
int main(void) {
mpz_class power = 1;
mpz_class modulo = 0;
mpz_class tens = 10;
mpz_class digitsum = 0;
for (int i = 1; i <= 1000; ++i) {
power *= 2;
}
while (power != 0) {
digitsum = digitsum + power % tens;
power = power / 10;
}
std::cout << digitsum << std::endl;
return EXIT_SUCCESS;
}

70
35/euler35.cpp Normal file
View file

@ -0,0 +1,70 @@
#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
void sieve(std::vector<int>& primes, int const limit) {
if (limit < 2) {
return;
} else if (limit < 3) {
primes.push_back(2);
return;
}
primes.push_back(2);
primes.push_back(3);
for (int num = 5; num <= limit; num += 2) {
int sqrtnum = (int) std::sqrt(num);
bool isPrime = true;
for (auto primeIt = ++primes.begin(); primeIt != primes.end() && *primeIt <= sqrtnum; ++primeIt) {
if (num % *primeIt == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primes.push_back(num);
}
}
}
int main(void) {
constexpr int limit = 1000000;
constexpr int PRIMELIMIT = limit - 1;
std::vector<int> primes;
sieve(primes, PRIMELIMIT);
std::vector<int> circular;
auto primeIt = primes.begin();
while (*primeIt < 10) {
circular.push_back(*primeIt);
++primeIt;
}
int tens = 10;
int nextTens = 100;
int rotations = 2;
while (primeIt != primes.end()) {
if (*primeIt >= nextTens) {
tens *= 10;
nextTens *= 10;
++rotations;
}
bool isCircular = true;
int prime = *primeIt;
for (int i = 1; i < rotations; ++i) {
prime = prime / 10 + (prime % 10) * tens;
isCircular = std::binary_search(primes.begin(), primes.end(), prime)? true: false;
if (!isCircular) {
break;
}
}
if (isCircular) {
circular.push_back(*primeIt);
}
++primeIt;
}
std::cout << circular.size() << std::endl;
}

48
87/euler87.R Normal file
View file

@ -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)

83
87/euler87.cpp Normal file
View file

@ -0,0 +1,83 @@
// C++-Lösung auf der Basis der C-Variante von Jörg Sommer
// Beinhaltet verbessertes Primzahlsieb
#include <cmath>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <algorithm>
/**
prime: Vektor zum Speichern der Primzahlen
limit: Obere Grenze des Bereichs in dem berechnet wird
Implementiert eine optimierte Version des Sieb des Eratosthenes.
Prüft nicht, ob die Grenze kleiner als 5 ist.
*/
static void sieve(std::vector<unsigned>& prime, unsigned const limit) {
prime.push_back(2u);
prime.push_back(3u);
for (unsigned num = 5; num <= limit; num += 2) {
unsigned sqrtnum = (unsigned) std::sqrt(num);
bool isPrime = true;
for (auto primeIt = ++prime.begin(); primeIt != prime.end() && *primeIt <= sqrtnum; ++primeIt) {
if (num % *primeIt == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
prime.push_back(num);
}
}
}
int main(void) {
constexpr unsigned LIMIT = 50000000;
constexpr unsigned PRIMELIMIT = (unsigned) std::sqrt(LIMIT);
std::vector<unsigned> primes;
primes.reserve(PRIMELIMIT / std::log(PRIMELIMIT)); // schätze Primzahldichte
sieve(primes, PRIMELIMIT);
std::cout << "Es wurden " << primes.size() << " Primzahlen gefunden" << std::endl;
std::vector<unsigned> numbers;
for (auto p4 = primes.begin(); p4 != primes.end(); ++p4) {
unsigned pow4 = *p4 * *p4;
pow4 *= pow4;
if (pow4 >= LIMIT)
break;
for (auto p3 = primes.begin(); p3 != primes.end(); ++p3) {
unsigned sum = pow4 + *p3 * *p3 * *p3;
if (sum >= LIMIT)
break;
for (auto p2 = primes.begin(); p2 != primes.end(); ++p2) {
unsigned pow2 = *p2 * *p2;
unsigned sum2 = sum + pow2;
if (sum2 >= LIMIT) {
break;
}
numbers.push_back(sum2);
}
}
}
//Zähle vorkommende Elemente
unsigned count = 1;
std::sort(numbers.begin(), numbers.end());
for (auto n = ++numbers.begin(); n != numbers.end(); ++n) {
if (*(n - 1) != *n) {
++count;
}
}
std::cout << "Ergebnis: " << count << std::endl;
return EXIT_SUCCESS;
}