C++ Mathematical Expression Toolkit (ExprTk)
release
Toggle main menu visibility
Loading...
Searching...
No Matches
exprtk
exprtk_naive_primes.cpp
Go to the documentation of this file.
1
/*
2
**************************************************************
3
* C++ Mathematical Expression Toolkit Library *
4
* *
5
* ExprTk Primes Via The Naive Method *
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
primes_via_naive_method
()
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
typedef
exprtk::function_compositor<T>
compositor_t;
33
typedef
typename
compositor_t::function function_t;
34
35
compositor_t compositor;
36
37
compositor.
add
(
38
function_t(
"is_prime"
)
39
.var(
"x"
)
40
.expression
41
(
42
" switch "
43
" { "
44
" case x <= 1 : return [false]; "
45
" case frac(x) != 0 : return [false]; "
46
" case x == 2 : return [true ]; "
47
" default : "
48
" { "
49
" var prime_lut[81] := "
50
" { "
51
" 2, 3, 5, 7, 11, 13, 17, 19, 23, "
52
" 29, 31, 37, 41, 43, 47, 53, 59, 61, "
53
" 67, 71, 73, 79, 83, 89, 97, 101, 103, "
54
" 107, 109, 113, 127, 131, 137, 139, 149, 151, "
55
" 157, 163, 167, 173, 179, 181, 191, 193, 197, "
56
" 199, 211, 223, 227, 229, 233, 239, 241, 251, "
57
" 257, 263, 269, 271, 277, 281, 283, 293, 307, "
58
" 311, 313, 317, 331, 337, 347, 349, 353, 359, "
59
" 367, 373, 379, 383, 389, 397, 401, 409, 419 "
60
" }; "
61
" "
62
" var upper_bound := min(x - 1, trunc(sqrt(x)) + 1); "
63
" "
64
" for (var i := 0; i < prime_lut[]; i += 1) "
65
" { "
66
" if (prime_lut[i] >= upper_bound) "
67
" return [true]; "
68
" else if ((x % prime_lut[i]) == 0) "
69
" return [false]; "
70
" }; "
71
" "
72
" var lower_bound := prime_lut[prime_lut[] - 1] + 2; "
73
" "
74
" for (var i := lower_bound; i < upper_bound; i += 2) "
75
" { "
76
" if ((x % i) == 0) "
77
" { "
78
" return [false]; "
79
" } "
80
" } "
81
" }; "
82
" }; "
83
" "
84
" return [true]; "
85
));
86
87
const
std::string primes_via_naive_method_program =
88
" for (var i := 1; i < 10000; i += 1) "
89
" { "
90
" if (is_prime(i)) "
91
" { "
92
" println(i, ' is prime'); "
93
" } "
94
" }; "
;
95
96
exprtk::rtl::io::println<T>
println;
97
98
symbol_table_t& symbol_table = compositor.symbol_table();
99
symbol_table.add_function(
"println"
,println);
100
101
expression_t expression;
102
expression.register_symbol_table(symbol_table);
103
104
parser_t parser;
105
parser.compile(primes_via_naive_method_program,expression);
106
107
expression.value();
108
}
109
110
int
main
()
111
{
112
primes_via_naive_method<double>
();
113
return
0;
114
}
exprtk::expression
Definition
exprtk.hpp:21832
exprtk::function_compositor
Definition
exprtk.hpp:43057
exprtk::function_compositor::add
bool add(const std::string &name, const std::string &expression, const Sequence< std::string, Allocator > &var_list, const bool override=false)
Definition
exprtk.hpp:43655
exprtk::parser
Definition
exprtk.hpp:22525
exprtk::symbol_table
Definition
exprtk.hpp:20090
exprtk.hpp
primes_via_naive_method
void primes_via_naive_method()
Definition
exprtk_naive_primes.cpp:27
main
int main()
Definition
exprtk_naive_primes.cpp:110
exprtk::rtl::io::println
Definition
exprtk.hpp:44273
Generated by
1.17.0