Number Theory Libraries
When I've taught introductory number theory as part of courses on
"Discrete Maths" and "Algorithms", I've not found a single library
that covers the wide range of topics. As a consequence, my
examples use a number of different libraries, which
are listed below. They can be obtained separately from
the links below or you can download my
zipped collection of them all.
Libraries
- The standard Java classes:
java.lang.Math,
java.math.BigInteger, and
java.math.BigDecimal.
All the third-party libraries are built atop these
classes, which means they can be readily used together.
- Apache Commons Numbers, which holds the numerical features of
Apache
Commons Math. I only use about half of the Numbers
classes (i.e. combinatorics, core, fractions, and primes).
- The
java-math-library (JML) overlaps with Apache
Commons Numbers in many places, but has more support for factoring, gcd,
and extra BigInteger and BigDecimal functions. Its main
drawback is a lack of online documentation, although I've generated
its API documentation, which you can download
as a zip file.
- Big-Math
has a large collection of BigDecimal functions, although
I've only employed its trigonometry features.
- An important aspect of teaching number theory is being able to
visualize the concepts, and I've employed three libraries in that role:
StdDraw,
Simple
Java Plot, and
Console Table.
I've described the first two on other pages on this website, while
Console Table is a simple (but very useful) class for
generating ASCII formatted tables (e.g. see
RecipsTable.java).
The examples that generate images include "Screenshot" links.
- Even with all of these libraries, I've still found it necessary
to include a small set of my own utilities in
Nut.java (Nut is short for
"NUmber Theory").
The examples are separated into several categories, but
you may prefer to download all the code as a
single zip file.
Special Numbers
- NumberTypes.java
prints details about Java's built-in math-related types.
- NumbersTest.java tests
a variety of math operations, separated into four groups:
BigDecimal Pitfalls, Constants, Nut Functions
(Nut.java is my
utility class), and Big-Math.
- Amicable.java
prints all the Amicable number pairs between 1 and 3000.
- Tau.java prints the first 100
Tau numbers starting from 1.
- Roman.java
converts an integer into Roman numerals, and back again.
- FermatNumbers.java
generates the first eight Fermat numbers using two approaches.
Series
Pi
- CalcPi.java
calculates Pi using three different Machin-like formulae,
and compares them with the Nut.PI 100 dp constant.
The digit spread is also displayed as a
vectorgram.
- pi10000.txt:
lists Pi to 10,000 digits, as produced by
Peter
Alfeld.
- PiMonte.java
estimates Pi using Monte Carlo simulation.
- WallisPi.java
evaluates the Wallis formula for Pi.
Fractions
Binomials
Modulo
- ModGCD.java
test GCD, modulo, the Chinese remainder theorem, and related
operations.
- ModPowerTable.java
generates a table of the modPow function,
marking the rows containing primitive roots, and
draws a grayscale
image of the table in a separate window.
- DrawCircle.java creates
animated string art
inspired by the Mathologer video
"Times Tables, Mandelbrot and the Heart of Mathematics".
- PynTriplets.java
outputs a series of Pythagorean triples using the
(m, n) generators.
Screenshot.
Each triplet is stored as a
PynTriplet object.
- CheckISBN.java
checks whether a given ten-digit string
is a valid ISBN.
- DayOfWeek.java
read in the day, month, and year, and calculates the
week day according to the Gregorian calendar.
- MagicSquare.java
generates odd-sized magic squares, in the range
1 to n2.
Primes
Ciphers
- Caesar.java
implements encoding and decoding using the Caesar cipher.
- Vigenere.java
generalizes the Caesar cipher, implementing the Vigenere cipher.
- RSA.java
performs encoding and decoding using the RSA cipher.
Utilities
Other Downloads and Links
- Batch files to easily compile and run my code:
compile.bat and
run.bat
- All the examples in one
zipped file (1.79 MB)
- My copies of the
various libraries (1.92 MB).
Unzip the file, and place the libs folder in the
same directory as the examples and batch files.
- JML
is a little lacking in online documentation, although the source
code includes examples and annotations for the
API. My copies of those API documents can be downloaded
as a zip file (1.32 MB).
- Apache
Commons Numbers has an excellent
user
guide, and
online documentation.
I only use about half of its libraries –
combinatorics, core, fractions, and primes –
although I'm sure its
complex, field, and gamma classes would be useful
for more complicated number theory tasks.
- Big-math's
online documentation can be found
here. I've only
used the trigonometry functions from the
BigDecimalMath class.
- Two great sources for number theory problems are
Project Euler and
Rosetta
Code. Many, many Java solutions for Euler questions
can be found at Project Nayuki, along with code written in Haskell,
Mathematica, and Python. Rosetta Code can be easily searched; a good
starting point is its
Prime
Numbers category.
- I'm sometimes asked for a good beginners book
on number theory. I recommend
Number Theory:
A Lively Introduction with Proofs, Applications, and Stories
by James E. Pommersheim, Tim K. Marks, and Erica L. Flapan, Wiley,
2010.
Dr. Andrew Davison
E-mail: ad@coe.psu.ac.th
Back to the third-party libraries page