Prime Numbers

FOR COMMERCIAL USE, PLEASE ENSURE YOU HAVE A COMMERCIAL LICENSE.
ANOTHER WAY TO SUPPORT MY WORK IS BY PURCHASING MY BOOK, CUSTOMER FIRST, VALUE NEXT.
THANK YOU FOR SUPPORTING MY WORK!

The tutorial consists of more than 200 live examples from 50 sections given separately for JAVA, C# and C++. Each of the examples can be copied and run on your own environment. In addition, mXparser provides an extensive collection of over 500 built-in math functions, expressions and symbols. Familiarize yourself with the scope and the syntax. Live testing is the best way to learn. Good luck! 🙂

Tutorial Math Collection API spec Download

Below is the code for JAVA, C# (the code for C# is almost identical) and C++. To copy the code, double-click inside the frame.

Case 1: Primality test / Prime test function

$$\text{IsPrime}(n)$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

/* Primality test function used in expression. */
Expression e1 = new Expression("ispr(5)");
Expression e2 = new Expression("ispr(9)");

/* Calculation and result output */
mXparser.consolePrintln("Res 1: " + e1.getExpressionString() + " = " + e1.calculate());
mXparser.consolePrintln("Res 2: " + e2.getExpressionString() + " = " + e2.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...

/* Primality test function used in expression. */
ExpressionPtr e1 = new_Expression("ispr(5)");
ExpressionPtr e2 = new_Expression("ispr(9)");

/* Calculation and result output */
mXparser::consolePrintln("Res 1: " + e1->getExpressionString() + " = " + e1->calculate());
mXparser::consolePrintln("Res 2: " + e2->getExpressionString() + " = " + e2->calculate());
Code result
[mXparser-v.5.2.1] Res 1: ispr(5) = 1.0
[mXparser-v.5.2.1] Res 2: ispr(9) = 0.0

Case 2: Primes counting function

$$\pi(n)$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

/* Prime counting function used in expression. */
Expression e = new Expression("Pi(10000000)");

/* Calculation and result output */
mXparser.consolePrintln("Res : " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Time: " + e.getComputingTime() + " s.");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...

/* Prime counting function used in expression. */
ExpressionPtr e = new_Expression("Pi(10000000)");

/* Calculation and result output */
mXparser::consolePrintln("Res : " + e->getExpressionString() + " = " + e->calculate());
mXparser_consolePrintln("Time: " + e->getComputingTime() + " s.");
Code result
[mXparser-v.5.2.1] Res : Pi(10000000) = 664579.0
[mXparser-v.5.2.1] Time: 10.748 s.

Case 3: Using built-in primes cache to accelerate calculations

$$\pi(n)$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

/* Primes cache initialization */
mXparser.initPrimesCache(10000000);
mXparser.consolePrintln("Primes cache init time: " + mXparser.primesCache.getComputingTime());

/* Prime counting function used in expression. */
Expression e = new Expression("Pi(10000000)");

/* Calculation and result output */
mXparser.consolePrintln("Res : " + e.getExpressionString() + " = " + e.calculate());
mXparser.consolePrintln("Time: " + e.getComputingTime() + " s.");
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...

/* Primes cache initialization */
mXparser::initPrimesCache(10000000);
mXparser_consolePrintln("Primes cache init time: " + mXparser::primesCache->getComputingTime());

/* Prime counting function used in expression. */
ExpressionPtr e = new_Expression("Pi(10000000)");

/* Calculation and result output */
mXparser::consolePrintln("Res : " + e->getExpressionString() + " = " + e->calculate());
mXparser_consolePrintln("Time: " + e->getComputingTime() + " s.");
Code result
[mXparser-v.5.2.1] Primes cache init time: 0.115
[mXparser-v.5.2.1] Res : Pi(10000000) = 664579.0
[mXparser-v.5.2.1] Time: 0.217 s.

Case 4: (Using case 3) Estimating number of primes using Offset logarithmic integral function

$$\frac{\pi(n)}{\text{Li}(x)}$$

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...

/* Primes cache was initialized in the previous example
 * and it will be used in the following calculations.
 *
 * mXparser.initPrimesCache(10000000);
 *
 * We are going to estimate the number of primes Pi(n)
 * using offset logarithmic integral function Li(n)
 */
Expression e = new Expression("Pi(10000000) / Li(10000000)");

/* Calculation and result output */
mXparser.consolePrintln("Res : " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...

/* Primes cache was initialized in the previous example
 * and it will be used in the following calculations.
 *
 * mXparser::initPrimesCache(10000000);
 *
 * We are going to estimate the number of primes Pi(n)
 * using offset logarithmic integral function Li(n)
 */
ExpressionPtr e = new_Expression("Pi(10000000) / Li(10000000)");

/* Calculation and result output */
mXparser::consolePrintln("Res : " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res : Pi(10000000) / Li(10000000) = 0.9994911249048335

Case 5: Prime factorization

Java/C# code
// JAVA: import org.mariuszgromada.math.mxparser.*;
// C#: using org.mariuszgromada.math.mxparser;
// ...
Argument x = new Argument("x = 12345");
Expression e = new Expression("sgn(x) * prod(i, 1, nfact(x), factval(x, i)^factexp(x, i) )", x);

mXparser.consolePrintln("Res 1: " + e.getExpressionString() + " = " + e.calculate());
C++ code
#include "org/mariuszgromada/math/mxparser.hpp"
// ...
ArgumentPtr x = new_Argument("x = 12345");
ExpressionPtr e = new_Expression("sgn(x) * prod(i, 1, nfact(x), factval(x, i)^factexp(x, i) )", x);

mXparser::consolePrintln("Res 1: " + e->getExpressionString() + " = " + e->calculate());
Code result
[mXparser-v.5.2.1] Res 1: sgn(x) * prod(i, 1, nfact(x), factval(x, i)^factexp(x, i) ) = 12345.0
Nuget – Package Manager (C#, F#, Visual Basic, …)

Install-Package MathParser.org-mXparser-Version 6.1.1

Nuget – .NET CLI

dotnet add package MathParser.org-mXparser--version 6.1.1

Nuget – Package Reference

<PackageReference Include="MathParser.org-mXparser"Version="6.1.1"/>

Maven – Dependency (Java, Kotlin, Scala, Groovy, …)

<dependency>
<groupid>
org.mariuszgromada.math</groupid>
<artifactid>
MathParser.org-mXparser</artifactid>
<version>
6.1.1</version>
</dependency>

Maven – Gradle

implementation 'org.mariuszgromada.math:MathParser.org-mXparser:6.1.1'

CMake – Dependency / FetchContent (C++, MSVC, LLVM/Clang, GNU/GCC, MinGW, MSYS2, WSL, Windows, Linux, Unix, MacOS)

include(FetchContent)
FetchContent_Declare(
MathParserOrgMxParser
GIT_REPOSITORY
https://github.com/mariuszgromada/MathParser.org-mXparser.git
GIT_TAG
v.6.1.1
SOURCE_SUBDIR CURRENT/cpp/lib
)
FetchContent_MakeAvailable(
MathParserOrgMxParser)
mxparser_link(YourExecutable)

GitHub

git clone https://github.com/mariuszgromada/MathParser.org-mXparser

OTHER DOWNLOAD OPTIONS

Download latest release – v.6.1.0 Sagitara: .NET bin onlyDownload latest release – v.6.1.0 Sagitara: JAVA bin onlyDownload latest release – v.6.1.0 Sagitara: bin + doc

NEWS FROM MATHPARSER.ORG
SOURCE CODE

Source code .zipSource code .tar.gz
View on GitHubMathSpace.pl

My other creative spaces

DONATION
Did you find the software useful?
Please consider donation 🙂
DONATE