C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_prime_sieve_vectorized.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Sieve Of Eratosthenes Vectorized *
6 * Author: Arash Partow (1999-2024) *
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
26template <typename T>
28{
29 typedef exprtk::symbol_table<T> symbol_table_t;
30 typedef exprtk::expression<T> expression_t;
31 typedef exprtk::parser<T> parser_t;
32
33 const std::string sieve_of_eratosthenes_program =
34 " var sieve[10^8] := [false]; "
35 " var m := trunc(sqrt(sieve[])); "
36 " "
37 " sieve[0] := true; "
38 " sieve[1] := true; "
39 " "
40 " for (var i := 0; i <= m; i += 1) "
41 " { "
42 " if (false == sieve[i]) "
43 " { "
44 " assign(sieve, true, i^2, sieve[] - 1, i); "
45 " } "
46 " }; "
47 " "
48 " var prime_count := sieve[] - sum(sieve); "
49 " "
50 " prime_count == 5761455; ";
51
52 symbol_table_t symbol_table;
54
55 symbol_table.add_package(vector_package);
56
57 expression_t expression;
58 expression.register_symbol_table(symbol_table);
59
60 parser_t parser;
61 parser.compile(sieve_of_eratosthenes_program,expression);
62
63 exprtk::timer timer;
64 timer.start();
65
66 const T result = expression.value();
67
68 timer.stop();
69
70 printf("Result: %8.3f\n",result);
71
72 printf("Total time: %8.3fsec\n",timer.time());
73}
74
75int main()
76{
77 sieve_of_eratosthenes_vectorized<double>();
78 return 0;
79}
double time() const
Definition exprtk.hpp:43502
void sieve_of_eratosthenes_vectorized()