Prime Numbers

TO SUPPORT MY WORK, ORDER A COMMERCIAL LICENSE
THANK YOU!

Tutorial Math Collection API spec Download

Below is the code for JAVA, the code for C# is almost identical.

Case 1: Primality test / Prime test function

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

import 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());
[mXparser-v.5.0.0] Res 1: ispr(5) = 1.0
[mXparser-v.5.0.0] Res 2: ispr(9) = 0.0

Case 2: Primes counting function

$$\pi(n)$$

import 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.");
[mXparser-v.5.0.0] Res : Pi(10000000) = 664579.0
[mXparser-v.5.0.0] Time: 10.172 s.

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

$$\pi(n)$$

import 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.");
[mXparser-v.5.0.0] Primes cache init time: 0.093
[mXparser-v.5.0.0] Res : Pi(10000000) = 664579.0
[mXparser-v.5.0.0] Time: 0.109 s.

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

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

import org.mariuszgromada.math.mxparser.*;
...
/* Primes cache was initialized in the previous example
 * and it will be used in the following calculations.
 *
 * 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());
[mXparser-v.5.0.0] Res : Pi(10000000) / Li(10000000) = 0.9994911249048335

Case 5: Prime factorization

import 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());
[mXparser-v.5.0.0] Res 1: sgn(x) * prod(i, 1, nfact(x), factval(x, i)^factexp(x, i) ) = 12345.0
Nuget
Install-Package MathParser.org-mXparser -Version 5.0.2
Maven
<dependency>
<groupid>org.mariuszgromada.math</groupid>
<artifactid>MathParser.org-mXparser</artifactid>
<version>5.0.2</version>
</dependency>
Gradle
implementation 'org.mariuszgromada.math:MathParser.org-mXparser:5.0.2'
Gradle (Kotlin)
implementation("org.mariuszgromada.math:MathParser.org-mXparser:5.0.2")
GitHub
git clone https://github.com/mariuszgromada/MathParser.org-mXparser
OTHER DOWNLOAD OPTIONS

Download latest release – v.5.0.2 Leonis: bin + docDownload latest release – v.5.0.2 Leonis: bin only, includes separate binaries for various .NET platforms and Java versions
Source code .zipSource code .tar.gz
View on GitHubMathSpace.pl

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