C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_prime_sieve.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk Sieve Of Eratosthenes *
6
* Author: Arash Partow (1999-2025) *
7
* URL: https://www.partow.net/programming/exprtk/index.html *
8
* *
9
* Copyright notice: *
10
* Free use of the Mathematical Expression Toolkit Library is *
11
* permitted under the guidelines and in accordance with the *
12
* most current version of the MIT License. *
13
* https://www.opensource.org/licenses/MIT *
14
* SPDX-License-Identifier: MIT *
15
* *
16
**************************************************************
17
*/
18
19
20
#include <cstdio>
21
#include <string>
22
23
#include "
exprtk.hpp
"
24
25
26
template
<
typename
T>
27
void
sieve_of_eratosthenes
()
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
}
70
71
int
main
()
72
{
73
sieve_of_eratosthenes<double>
();
74
return
0;
75
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::parser::compile
bool compile(const std::string &expression_string, expression< T > &expr)
Definition
exprtk.hpp:24826
exprtk::timer
Definition
exprtk.hpp:44056
exprtk::timer::stop
void stop()
Definition
exprtk.hpp:44103
exprtk::timer::time
double time() const
Definition
exprtk.hpp:44125
exprtk::timer::start
void start()
Definition
exprtk.hpp:44097
exprtk.hpp
sieve_of_eratosthenes
void sieve_of_eratosthenes()
Definition
exprtk_prime_sieve.cpp:27
main
int main()
Definition
exprtk_prime_sieve.cpp:71
Generated by
1.17.0