Package net.n1da.dev.euler.helper
Class LargeNumber
- java.lang.Object
-
- net.n1da.dev.euler.helper.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 themaximum
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.
-
Method Summary
Modifier and Type Method Description LargeNumber
add(LargeNumber num)
Adds the given number to the current objects and returns the sum of both.LargeNumber
clone()
Clones the current number to return a newLargeNumber
with the same values in itsdata
.int
compareTo(LargeNumber num)
Compares this object with the given one.boolean
equals(java.lang.Object o)
Checks, if the given object is equal to the current number.int
getDigit(int pos)
Reads and returns a digit at the wanted position.int
getLength()
Reads the length ofdata
to get the number of digits in this large number.LargeNumber
getWithSortedDigits()
Just sorts the values indata
.int
hashCode()
It calculates and returns the hashcode of this object.boolean
isPalindromic()
Checks if a large number is palindromic.boolean
isPermutationOf(LargeNumber num)
Checks if the given number is a permutation of the current object.LargeNumber
mult(int n)
Multiplies the current number with the given integer value.LargeNumber
mult(LargeNumber n)
Multiplies the current number with the givenLargeNumber
.static LargeNumber
parse(java.lang.String line)
Parses the given string and creates a newLargeNumber()
with this value.LargeNumber
pow(int exp)
Calculates the power of the current number by the given integer value.LargeNumber
replaceDigit(int search, int replace)
Replaces every occurrence of a wanted digit by another given digit.LargeNumber
reverse()
Creates a copy of this large number that has reverse ordered digits.LargeNumber
sub(LargeNumber num)
Subtracts the given number from the current objects and returns the difference of both.LargeNumber
sumOfDigitFactorials()
Calculates the sum of the factorials of all digits of thisLargeNumber
.LargeNumber
sumOfDigitPowers(int exp)
Calculates the sum of the power of all digits of thisLargeNumber
.LargeNumber
sumOfDigits()
Calculates the sum of all digits of thisLargeNumber
.java.lang.String
toString()
Return the current number asstring
.void
trim()
Deletes all leading zeros from the current number.
-
-
-
Field Detail
-
FACTORIALS
private static final int[] FACTORIALS
A memory that provides the factorial for the numbers from 0 to 9. Within methodsumOfDigitFactorials()
is saves some calculation time.- See Also:
sumOfDigitFactorials()
-
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 thanmax
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 themaximum
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 valuemax
- 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
- anotherLargeNumber()
- Returns:
- the sum
-
clone
public LargeNumber clone()
Clones the current number to return a newLargeNumber
with the same values in itsdata
.- Overrides:
clone
in classjava.lang.Object
- Returns:
- a new
LargeNumber
-
compareTo
public int compareTo(LargeNumber num)
Compares this object with the given one.- Specified by:
compareTo
in interfacejava.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 classjava.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 indata
-
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 ofdata
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 indata
.- Returns:
- A new
LargeNumber
with sorted digits
-
hashCode
public int hashCode()
It calculates and returns the hashcode of this object.- Overrides:
hashCode
in classjava.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
-
mult
public LargeNumber mult(LargeNumber n)
Multiplies the current number with the givenLargeNumber
.- Parameters:
n
- the number to multiply with- Returns:
- the product of the both
LargeNumber
s
-
parse
public static LargeNumber parse(java.lang.String line)
Parses the given string and creates a newLargeNumber()
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 replacedreplace
- 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
- anotherLargeNumber()
- Returns:
- the difference
-
sumOfDigits
public LargeNumber sumOfDigits()
Calculates the sum of all digits of thisLargeNumber
.- Returns:
- the cross total
-
sumOfDigitFactorials
public LargeNumber sumOfDigitFactorials()
Calculates the sum of the factorials of all digits of thisLargeNumber
. If this number isn = 123
this method will returns = 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 thisLargeNumber
. If this number isn = 123
this method will returns = 12+22+32
with given parameterexp = 2
!- Parameters:
exp
- the exponent to calculate the power with- Returns:
- the cross total
-
toString
public java.lang.String toString()
Return the current number asstring
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- the current number as
string
-
trim
public void trim()
Deletes all leading zeros from the current number.
-
-