C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_sumofprimes.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Summation of Prime Factors *
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 typedef exprtk::function_compositor<T> compositor_t;
33 typedef typename compositor_t::function function_t;
34
35 symbol_table_t symbol_table;
36
37 compositor_t compositor(symbol_table);
38
39 // define function: sum_of_primes(z)
40 compositor.add(
41 function_t("sum_of_primes")
42 .var("z")
43 .expression(
44 " var i := 2; "
45 " var total := 0; "
46 " while (z > 1) "
47 " { "
48 " if (0 == (z % i)) "
49 " { "
50 " total += i; "
51 " z /= i; "
52 " } "
53 " else "
54 " i += 1; "
55 " }; "
56 " total; "));
57
59
60 symbol_table.add_function("println",println);
61
62 const std::string sum_of_prime_factors_program =
63 " for (var i := 1; i <= 100; i += 1) "
64 " { "
65 " println(i, sum_of_primes(i)); "
66 " } ";
67
68 expression_t expression;
69 expression.register_symbol_table(symbol_table);
70
71 parser_t parser;
72 parser.compile(sum_of_prime_factors_program,expression);
73
74 expression.value();
75}
76
77int main()
78{
79 sum_of_primes<double>();
80 return 0;
81}
void sum_of_primes()
int main()