IntegerFunction.java
(Java pseudo class)
Author:Conrad Weisert, Information Disciplines, Inc., Chicago Purpose:A collection of useful functions that operate on one or two integers,
analogous to the standard library 
OAQs (Occasionally Asked Questions)

Note: In all of the following examples, the
qualifier name of the
pseudoclass, IntegerFunction
is omitted
for readability. You should (a) insert it as a qualifier before
each of the function names, (b) give the pseudoclass
a shorter name and use it, or (c) move the source code for the functions
you need into one of your own classes.
public static String toEnglish(long n)
This function converts a nonnegative integer
to its representation in English words. For example, if k=12345678
then toEnglish(k)
yields the string:
"twelve million, three hundred fortyfive thousand, six hundred seventyeight",
and toEnglish(0)
yields "zero".
Since the result string may be longer than a text box or report column,
the user may have to parse it to break it into lines that fit.
Note that this function is not amenable to internationalization, since in many languages the form of a number in words is dependent on context.
public static String toString(long n, int b)
public static String toString(long n)
This function converts an integer to its string representation in base
2 ≤ b ≤ 16. For example if k=50, then
toString(k,2)
yields "110010" and
toString(k,16)
yields "32". The
singleparameter version assumes b=10.
public static String toRoman(int n)
If 1 ≤ n ≤ 3999, the result is the Roman numeral representation.
Otherwise the Arabic numeral result is returned, i.e. the same as
toString(n)
.
public static long[] primeFactors(long n)
The argument must be greater than 1.
Upon return result[0]
contains the number of prime factors,
and
result[1] . . . result[result[0]]
contain the factors
in ascending order. For example, if you
code:
long result[] = primeFactors(60);then result will contain
{4, 2, 2, 3, 5}
,
while long result[] = primeFactors(61);will yield
{1, 61}
.
Remaining elements of the result array are undefined. (The function
always allocates a 64element array, in order to accommodate the largest
possible number of factors of a 63bit long integer).
Suggestions from Curt Sampson and Ravi Venkataraman improved the internal quality of this function.
Return to top of this page
public static long gcd(long m, long n) public static long lcm(long m, long n)
Both arguments must be positive. The gcd
function implements Euclid's fastconverging algorithm recursively.
Overflow is possible in lcm
, which
uses gcd
.
public static double power (double x, int n)
Java's standard Math
pseudoclass
contains an exponentiation function pow
that takes a floatingpoint exponent parameter. This function, on the
other hand, caters to the common case where the exponent is an integer.
It may or may not execute faster than the more general function.
(It is also interesting to students as an example of both recursion and
functional notation.)
public static long min (long m, long n)
public static long max (long m, long n)
public static long abs (long n)
These three duplicate functions found in Java's Math
pseudoclass. They're included here for convenience and possible consistency.
Return to Freeware directory
Return to IDI home page
Last modified August, 2003