C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Functions
exprtk_prime_sieve.cpp File Reference
#include <cstdio>
#include <string>
#include "exprtk.hpp"
Include dependency graph for exprtk_prime_sieve.cpp:

Go to the source code of this file.

Functions

template<typename T >
void sieve_of_eratosthenes ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 71 of file exprtk_prime_sieve.cpp.

72{
73 sieve_of_eratosthenes<double>();
74 return 0;
75}

◆ sieve_of_eratosthenes()

template<typename T >
void sieve_of_eratosthenes ( )

Definition at line 27 of file exprtk_prime_sieve.cpp.

28{
29 typedef exprtk::expression<T> expression_t;
30 typedef exprtk::parser<T> parser_t;
31
32 const std::string sieve_of_eratosthenes_program =
33 " var sieve[10^8] := [false]; "
34 " var m := trunc(sqrt(sieve[])); "
35 " "
36 " sieve[0] := true; "
37 " sieve[1] := true; "
38 " "
39 " for (var i := 0; i <= m; i += 1) "
40 " { "
41 " if (false == sieve[i]) "
42 " { "
43 " for (var j := i^2; j < sieve[]; j += i) "
44 " { "
45 " sieve[j] := true; "
46 " } "
47 " } "
48 " }; "
49 " "
50 " var prime_count := sieve[] - sum(sieve); "
51 " "
52 " prime_count == 5761455; ";
53
54 expression_t expression;
55
56 parser_t parser;
57 parser.compile(sieve_of_eratosthenes_program,expression);
58
59 exprtk::timer timer;
60 timer.start();
61
62 const T result = expression.value();
63
64 timer.stop();
65
66 printf("Result: %8.3f\n",result);
67
68 printf("Total time: %8.3fsec\n",timer.time());
69}
bool compile(const std::string &expression_string, expression< T > &expr)
Definition exprtk.hpp:24443
double time() const
Definition exprtk.hpp:43502

References exprtk::parser< T >::compile(), exprtk::timer::start(), exprtk::timer::stop(), and exprtk::timer::time().

Here is the call graph for this function: