Prime Numbers

mXparser - logo

*** If you found the software useful donation is something you might consider πŸ™‚ ***

If you found the software useful donation is something you might consider :-)

JAVA code

Case 1: Primality 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.2.3.1] Res 1: ispr(5) = 1.0
[mXparser-v.2.3.1] 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.2.3.1] Res : Pi(10000000) = 664579.0
[mXparser-v.2.3.1] Time: 9.525 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.2.3.1] Primes cache init time: 0.295
[mXparser-v.2.3.1] Res : Pi(10000000) = 664579.0
[mXparser-v.2.3.1] Time: 0.272 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.2.3.1] 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.4.1.0] Res 1: sgn(x) * prod(i, 1, nfact(x), factval(x, i)^factexp(x, i) ) = 12345.0

*** If you found the software useful donation is something you might consider πŸ™‚ ***

If you found the software useful donation is something you might consider :-)

Enjoy! πŸ™‚

 

Best regards,

Mariusz Gromada

Download latest release – v.4.1.1 Aeries: bin + doc + src (.zip 13.4 MB)

Download latest release – v.4.1.1 Aeries: bin only (.zip 4.35 MB) – separate binaries for java: 1.5, 1.6, 1.7, 1.8; .NET: 2.0, 3.0, 3.5, 4.0, 4.5, 4.6, 4.7, .NET Core, .NET Standard, .NET PCL, Xamarin.Android, Xamarin.iOS

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