-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83c937c
commit 1df011f
Showing
4 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifdef __cplusplus | ||
#include "../../src/algorithms/number_theory/mersenne_primes.h" | ||
#endif | ||
|
||
int main() { | ||
int64_t n = 100; | ||
std::vector<int> first_10_mersenne_primes = mersenne(10000000); | ||
for (auto &x : first_10_mersenne_primes) { | ||
std::cout << x << " "; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef ERATOSTHENES_SIEVE_H | ||
#define ERATOSTHENES_SIEVE_H | ||
|
||
#ifdef __cplusplus | ||
#include <assert.h> | ||
#include <iostream> | ||
#include <vector> | ||
#endif | ||
|
||
std::vector<bool> soe(int64_t n) { | ||
assert(n != 0); | ||
std::vector<bool> prime(n + 1, true); | ||
for (int64_t p = 2; p * p <= n; ++p) { | ||
if (prime[p]) { | ||
for (int64_t i = p * p; i <= n; i += p) { | ||
prime[i] = false; | ||
} | ||
} | ||
} | ||
return prime; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef MERSENNE_PRIMES_H | ||
#define MERSENNE_PRIMES_H | ||
|
||
#ifdef __cplusplus | ||
#include "eratosthenes_sieve.h" | ||
#include <iostream> | ||
#endif | ||
|
||
/* | ||
*mersenne function. | ||
*@param n: the upper bound | ||
*Returns vector<int>: the vector includes the elements "p" from m = 2^p - 1 for | ||
*which p gives us a mersenne prime number. | ||
*/ | ||
std::vector<int> mersenne(int64_t n) { | ||
std::vector<bool> prime = soe(n); | ||
std::vector<int> elements; | ||
int64_t k = 2; | ||
while ((1 << k) - 1 <= n) { | ||
if (prime[(1 << k) - 1]) { | ||
elements.push_back(k); | ||
} | ||
k++; | ||
} | ||
return elements; | ||
} | ||
#endif |