-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert rust prime generator to C++, solve p7 (untested)
- Loading branch information
1 parent
28940dd
commit 0e7edeb
Showing
11 changed files
with
200 additions
and
13 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
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
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,73 @@ | ||
#pragma once | ||
|
||
#include <iostream> | ||
#include <vector> | ||
#include <map> | ||
#include <limits> | ||
|
||
template<typename T> | ||
class PrimeGenerator { | ||
public: | ||
PrimeGenerator() | ||
: prime(T(0)), candidate(T(1) + T(1)), has_limit(false), limit(std::numeric_limits<T>::max()) {} | ||
|
||
PrimeGenerator(T upper_limit) | ||
: prime(T(0)), candidate(T(1) + T(1)), has_limit(true), limit(upper_limit) {} | ||
|
||
T next() { | ||
if (has_limit && prime >= limit) { | ||
map.clear(); | ||
return T(-1); | ||
} | ||
prime = next_prime(candidate); | ||
candidate = prime + T(1); | ||
return prime; | ||
} | ||
|
||
bool has_next() const { | ||
return !has_limit || prime < limit; | ||
} | ||
|
||
private: | ||
std::map<T, std::vector<T> > sieve; | ||
T prime; | ||
T candidate; | ||
bool has_limit; | ||
T limit; | ||
|
||
T next_prime(T candidate) { | ||
typename std::map<T, std::vector<T> >::iterator it = sieve.find(candidate); | ||
if (it != sieve.end()) { | ||
const std::vector<T>& numbers = it->second; | ||
for (typename std::vector<T>::const_iterator num = numbers.begin(); num != numbers.end(); ++num) { | ||
const T num_val = *num; | ||
typename std::map<T, std::vector<T> >::iterator entry_it = sieve.find(candidate + num_val); | ||
if (entry_it != sieve.end()) | ||
entry_it->second.push_back(num_val); | ||
else { | ||
std::vector<T> new_vec; | ||
new_vec.push_back(num_val); | ||
sieve[candidate + num_val] = new_vec; | ||
} | ||
} | ||
sieve.erase(candidate); | ||
return next_prime(candidate + T(1)); | ||
} | ||
else { | ||
std::vector<T> vec; | ||
vec.push_back(candidate); | ||
sieve[candidate * candidate] = vec; | ||
return candidate; | ||
} | ||
} | ||
}; | ||
|
||
template<typename T> | ||
PrimeGenerator<T> primes() { | ||
return PrimeGenerator<T>(); | ||
} | ||
|
||
template<typename T> | ||
PrimeGenerator<T> primes_until(T x) { | ||
return PrimeGenerator<T>(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
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,37 @@ | ||
/* | ||
Project Euler Problem 7 | ||
The prime number infrastructure paid off here | ||
Problem: | ||
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. | ||
What is the 10 001st prime number? | ||
*/ | ||
|
||
#ifndef EULER_P0007 | ||
#define EULER_P0007 | ||
#include <stdint.h> | ||
#include <iostream> | ||
#include "include/macros.hpp" | ||
#include "include/primes.hpp" | ||
|
||
uint32_t EMSCRIPTEN_KEEPALIVE p0007() { | ||
uint32_t answer, count = 0; | ||
PrimeGenerator<uint32_t> ps = primes(); | ||
while (true) { | ||
answer = ps.next(); | ||
if (++count == 10001) | ||
break; | ||
} | ||
return answer; | ||
} | ||
|
||
#ifndef UNITY_END | ||
int main(int argc, char const *argv[]) { | ||
std::cout << p0007() << std::endl; | ||
return 0; | ||
} | ||
#endif | ||
#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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,9 +25,7 @@ | |
1, | ||
2, | ||
4, | ||
6, | ||
8, | ||
9, | ||
*range(6, 10), | ||
11, | ||
*range(13, 18), | ||
*range(19, 21), | ||
|
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,43 @@ | ||
primes.hpp | ||
========== | ||
|
||
View source code :source:`cplusplus/src/include/primes.hpp` | ||
|
||
Includes | ||
-------- | ||
|
||
- :external:cpp:type:`iostream` | ||
- :external:cpp:type:`vector` | ||
- :external:cpp:type:`map` | ||
- :external:cpp:type:`limits` | ||
|
||
.. cpp:namespace-push:: primes | ||
.. cpp:class:: PrimeGenerator | ||
|
||
.. cpp:function:: PrimeGenerator<T> PrimeGenerator<T>() | ||
.. cpp:function:: PrimeGenerator<T> primes<T>() | ||
|
||
These constructors will return an infinite generator of prime numbers. Note, however, that it does not guard | ||
against overflow, so choose your type carefully. | ||
|
||
.. cpp:function:: PrimeGenerator<T> PrimeGenerator<T>(T upper_limit) | ||
.. cpp:function:: PrimeGenerator<T> primes_until<T>(T upper_limit) | ||
|
||
These constructors will return a finite generator of prime numbers, going to ``upper_limit``. | ||
|
||
.. cpp:function:: bool next() | ||
|
||
Returns the next prime, or ``-1`` if it is above the defined limit. | ||
|
||
.. cpp:function:: bool has_next() | ||
|
||
Returns ``true`` if there is a next value to generate. | ||
|
||
.. cpp:namespace-pop:: | ||
|
||
.. literalinclude:: ../../../../cplusplus/src/include/primes.hpp | ||
:language: C++ | ||
:linenos: | ||
|
||
.. tags:: cpp-iterator, prime-number |
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,31 @@ | ||
C++ Implementation of Problem 7 | ||
=============================== | ||
|
||
View source code :source:`cplusplus/src/p0007.cpp` | ||
|
||
Includes | ||
-------- | ||
|
||
- `"macros.hpp" <./lib/macros.html>`__ | ||
- `"primes.hpp" <./lib/primes.html>`__ | ||
- :external:c:type:`stdint` | ||
- :external:cpp:type:`iostream` | ||
|
||
Solution | ||
-------- | ||
|
||
.. cpp:namespace-push:: p0007 | ||
|
||
.. cpp:function:: uint32_t p0007() | ||
|
||
.. cpp:function:: int main(int argc, char const *argv[]) | ||
|
||
.. note:: | ||
|
||
This function is only present in the Python test runner, or when compiling as a standalone program. | ||
|
||
.. literalinclude:: ../../../cplusplus/src/p0007.cpp | ||
:language: C++ | ||
:linenos: | ||
|
||
.. tags:: cpp-iterator, prime-number |
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