Class LargeNumber

  • All Implemented Interfaces:
    java.lang.Comparable<LargeNumber>

    public class LargeNumber
    extends java.lang.Object
    implements java.lang.Comparable<LargeNumber>
    A class for large numbers without any limits using an array of digits as internal storage.
    Since:
    23.04.2015 22:46:14
    Author:
    Nico Danneberg
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private int[] data
      The storage of the digits.
      private static int[] FACTORIALS
      A memory that provides the factorial for the numbers from 0 to 9.
      private int max
      Sometimes not all digits of a large number are necessary.
    • Constructor Summary

      Constructors 
      Constructor Description
      LargeNumber()
      Creates a large number with value zero.
      LargeNumber​(int value)
      Creates a large number with the given value and no restrictions to the maximum number of digits.
      LargeNumber​(int value, int max)
      Creates a large number with the given value that will never has more digits than the given maximum.
      LargeNumber​(java.lang.Integer[] digits)
      Creates a large number with the internal value of the given array.
    • Field Detail

      • data

        private int[] data
        The storage of the digits.
      • max

        private int max
        Sometimes not all digits of a large number are necessary. Due to performance reasons the current number is never greater than max digits.
    • Constructor Detail

      • LargeNumber

        public LargeNumber()
        Creates a large number with value zero.
      • LargeNumber

        public LargeNumber​(int value)
        Creates a large number with the given value and no restrictions to the maximum number of digits.
        Parameters:
        value - the initial value
      • LargeNumber

        public LargeNumber​(int value,
                           int max)
        Creates a large number with the given value that will never has more digits than the given maximum.
        Parameters:
        value - the initial value
        max - the maximum number of digits this number will every have
      • LargeNumber

        public LargeNumber​(java.lang.Integer[] digits)
        Creates a large number with the internal value of the given array.
        Parameters:
        digits - the initial value
    • Method Detail

      • add

        public LargeNumber add​(LargeNumber num)
        Adds the given number to the current objects and returns the sum of both.
        Parameters:
        num - another LargeNumber()
        Returns:
        the sum
      • clone

        public LargeNumber clone()
        Clones the current number to return a new LargeNumber with the same values in its data.
        Overrides:
        clone in class java.lang.Object
        Returns:
        a new LargeNumber
      • compareTo

        public int compareTo​(LargeNumber num)
        Compares this object with the given one.
        Specified by:
        compareTo in interface java.lang.Comparable<LargeNumber>
        Parameters:
        num - the number to compare with
        Returns:
        1 if this object if greater that, -1 if it's lower than, and 0 it it's equal to the given number
      • equals

        public boolean equals​(java.lang.Object o)
        Checks, if the given object is equal to the current number.
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - the object to test the value against the current number
        Returns:
        true, if the given object is a LargeNumber with the same values in data
      • getDigit

        public int getDigit​(int pos)
                     throws java.lang.ArrayIndexOutOfBoundsException
        Reads and returns a digit at the wanted position.
        Parameters:
        pos - the position to read the digit from
        Returns:
        a digit at the given position
        Throws:
        java.lang.ArrayIndexOutOfBoundsException
      • getLength

        public int getLength()
        Reads the length of data to get the number of digits in this large number.
        Returns:
        the length (count of digits) of the large number
      • getWithSortedDigits

        public LargeNumber getWithSortedDigits()
        Just sorts the values in data.
        Returns:
        A new LargeNumber with sorted digits
      • hashCode

        public int hashCode()
        It calculates and returns the hashcode of this object.
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hascode of the string representation of this object
        See Also:
        toString(), String.hashCode()
      • isPalindromic

        public boolean isPalindromic()
        Checks if a large number is palindromic.
        Returns:
        true if the number is a palindrom
      • isPermutationOf

        public boolean isPermutationOf​(LargeNumber num)
        Checks if the given number is a permutation of the current object.
        Parameters:
        num - the number to be checked
        Returns:
        true if every digit of data exists exactly once in both numbers
      • mult

        public LargeNumber mult​(int n)
        Multiplies the current number with the given integer value.
        Parameters:
        n - the number to multiply with
        Returns:
        the product of the given integer and the current object
      • parse

        public static LargeNumber parse​(java.lang.String line)
        Parses the given string and creates a new LargeNumber() with this value.
        Parameters:
        line - the string to parse
        Returns:
        a new large number
      • pow

        public LargeNumber pow​(int exp)
        Calculates the power of the current number by the given integer value.
        Parameters:
        exp - the exponent to power with
        Returns:
        the power of the current number by the given integer value
      • replaceDigit

        public LargeNumber replaceDigit​(int search,
                                        int replace)
        Replaces every occurrence of a wanted digit by another given digit.
        Parameters:
        search - the digit to be replaced
        replace - the new digit
        Returns:
        a new large number with replaced digits
      • reverse

        public LargeNumber reverse()
        Creates a copy of this large number that has reverse ordered digits.
        Returns:
        a new large number with reverse ordered digits
      • sub

        public LargeNumber sub​(LargeNumber num)
        Subtracts the given number from the current objects and returns the difference of both.
        Parameters:
        num - another LargeNumber()
        Returns:
        the difference
      • sumOfDigits

        public LargeNumber sumOfDigits()
        Calculates the sum of all digits of this LargeNumber.
        Returns:
        the cross total
      • sumOfDigitFactorials

        public LargeNumber sumOfDigitFactorials()
        Calculates the sum of the factorials of all digits of this LargeNumber. If this number is n = 123 this method will return s = 1!+2!+3! = 1+2+6 = 9!
        Returns:
        the cross total
      • sumOfDigitPowers

        public LargeNumber sumOfDigitPowers​(int exp)
        Calculates the sum of the power of all digits of this LargeNumber. If this number is n = 123 this method will return s = 12+22+32 with given parameter exp = 2!
        Parameters:
        exp - the exponent to calculate the power with
        Returns:
        the cross total
      • toString

        public java.lang.String toString()
        Return the current number as string.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the current number as string
      • trim

        public void trim()
        Deletes all leading zeros from the current number.