Skip to content

Instantly share code, notes, and snippets.

@franklsf95
Created November 17, 2014 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franklsf95/80ff11bf383aaf2ed52b to your computer and use it in GitHub Desktop.
Save franklsf95/80ff11bf383aaf2ed52b to your computer and use it in GitHub Desktop.
Big Integer exercise in Java
/*
* Adapted from java.math.BigInteger
*
* This class only support non-negative integers
*/
public class BigInt implements Comparable<BigInt> {
// Constructors
public BigInt(byte[] val) {
}
public BigInt(String val) {
}
// optional
public BigInt(long val) {
}
// Arithmetic Operations
/**
* Returns a BigInt whose value is {@code (this + val)}.
*
* @param val value to be added to this BigInt.
* @return {@code this + val}
*/
public BigInt add(BigInt val) {
}
// this is only a convenience method
public BigInt add(long val) {
}
/**
* Returns a BigInt whose value is {@code (this - val)}.
*
* @param val value to be subtracted from this BigInt.
* @return {@code this - val}
*/
public BigInt subtract(BigInt val) {
}
/**
* Returns a BigInt whose value is {@code (this * val)}.
*
* @param val value to be multiplied by this BigInt.
* @return {@code this * val}
*/
public BigInt multiply(BigInt val) {
}
// This can be implemented in a faster way
public BigInt multiply(long v) {
}
// optional
/**
* Returns an array of two BigInts containing {@code (this / val)}
* followed by {@code (this % val)}.
*
* @param val value by which this BigInt is to be divided, and the
* remainder computed.
* @return an array of two BigInts: the quotient {@code (this / val)}
* is the initial element, and the remainder {@code (this % val)}
* is the final element.
* @throws ArithmeticException if {@code val} is zero.
*/
public BigInt[] divideAndRemainder(BigInt val) {
}
// optional
/**
* Returns a BigInt whose value is <tt>(this<sup>exponent</sup>)</tt>.
* Note that {@code exponent} is an integer rather than a BigInt.
*
* @param exponent exponent to which this BigInt is to be raised.
* @return <tt>this<sup>exponent</sup></tt>
* @throws ArithmeticException {@code exponent} is negative. (This would
* cause the operation to yield a non-integer value.)
*/
public BigInt pow(int exponent) {
}
// optional
/**
* Returns a BigInt whose value is the greatest common divisor of
* {@code abs(this)} and {@code abs(val)}. Returns 0 if
* {@code this==0 && val==0}.
*
* @param val value with which the GCD is to be computed.
* @return {@code GCD(abs(this), abs(val))}
*/
public BigInt gcd(BigInt val) {
}
// Comparison Operations
/**
* Compares this BigInt with the specified BigInt. This
* method is provided in preference to individual methods for each
* of the six boolean comparison operators ({@literal <}, ==,
* {@literal >}, {@literal >=}, !=, {@literal <=}). The suggested
* idiom for performing these comparisons is: {@code
* (x.compareTo(y)} &lt;<i>op</i>&gt; {@code 0)}, where
* &lt;<i>op</i>&gt; is one of the six comparison operators.
*
* @param val BigInt to which this BigInt is to be compared.
* @return -1, 0 or 1 as this BigInt is numerically less than, equal
* to, or greater than {@code val}.
*/
public int compareTo(BigInt val) {
}
public String toString() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment