C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
exprtk_pythagorean_triples.cpp
Go to the documentation of this file.
1/*
2 **************************************************************
3 * C++ Mathematical Expression Toolkit Library *
4 * *
5 * ExprTk Pythagorean Triples *
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 pythagorean_triples_program =
34 " const var num_triples := 1000; "
35 " var triple_count := 0; "
36 " var n := 2; "
37 " "
38 " while (true) "
39 " { "
40 " for (var i := 1; i < n; i += 1) "
41 " { "
42 " var a := n^2 - i^2; "
43 " var b := 2 * n * i; "
44 " var c := n^2 + i^2; "
45 " "
46 " if (a > b) a <=> b; "
47 " if (b > c) b <=> c; "
48 " if (a > b) a <=> b; "
49 " "
50 " println('(', a ,',', b ,',', c ,')'); "
51 " "
52 " if ((triple_count += 1) >= num_triples) "
53 " { "
54 " return []; "
55 " }; "
56 " }; "
57 " "
58 " n += 1; "
59 " } ";
60
62
63 symbol_table_t symbol_table;
64 symbol_table.add_function ("println", println);
65
66 expression_t expression;
67 expression.register_symbol_table(symbol_table);
68
69 parser_t parser;
70 parser.compile(pythagorean_triples_program, expression);
71
72 expression.value();
73}
74
75int main()
76{
77 exprtk_pythagorean_triples<double>();
78 return 0;
79}
void exprtk_pythagorean_triples()