Class NumberUtil

java.lang.Object
org.drip.numerical.common.NumberUtil

public class NumberUtil
extends java.lang.Object
NumberUtil implements number utility functions. It exposes the following functions:

  • Verify number/number array validity, and closeness/sign match
  • Factorial Permutation/Combination functionality
  • Dump multi-dimensional array contents
  • Min/Max/Bound the array entries within limits




Author:
Lakshmi Krishnamurthy
  • Constructor Summary

    Constructors
    Constructor Description
    NumberUtil()  
  • Method Summary

    Modifier and Type Method Description
    static int BinaryDigitCount​(int n)
    Retrieve the Binary Digit Count
    static double Bound​(double dblX, double dblFloor, double dblCeiling)
    Bound the input to within (floor, Ceiling), i.e., compute Min (Max (floor, X), Ceiling)
    static boolean CanMeasureWater​(int x, int y, int z)
    You are given two jugs with capacities x and y liters.
    static int DivideIntegers​(int dividend, int divisor)
    Divide two integers without using multiplication, division, and mod operator.
    static double DoubleFactorial​(int n)
    Compute (2n - 1)!!
    static int Factorial​(int n)
    This function implements Factorial N.
    static double Fractional​(double z)
    Retrieve the Fractional Part of z
    static long GCD​(long a, long b)
    Greatest Common Divisor between Two Numbers
    static double HalfDownShiftedFactorial​(int n)
    Compute (n - 0.5)!
    static boolean IsInteger​(double z)
    Indicate if z is an Integer
    static boolean IsNegativeInteger​(double z)
    Indicate if z is a Negative Integer
    static boolean IsNonNegativeInteger​(double z)
    Indicate if z is a Non-Negative Integer
    static boolean IsNonPositiveInteger​(double z)
    Indicate if z is a Non-Positive Integer
    static boolean IsNumberSequenceAdditive​(java.lang.String s)
    Additive number is a string whose digits can form additive sequence.
    static boolean IsPositiveInteger​(double z)
    Indicate if z is a Positive Integer
    static boolean IsValid​(double dbl)
    Checks if the input double is Infinite or NaN
    static boolean IsValid​(double[] adbl)
    Checks if the input double array contains an Infinite or an NaN
    static boolean IsValid​(long l)
    Check if the Input Long is MIN_VALUE or MAX_VALUE
    static boolean IsValid​(long[] al)
    Check if the Input Long Array contains a MIN_VALUE or MAX_VALUE
    static double Maximum​(double[] adbl)
    Retrieve the Maximum Element in the specified Array
    static double Minimum​(double[] adbl)
    Retrieve the Minimum Element in the specified Array
    static int NCK​(int n, int k)
    This function implements N choose K.
    static int NextPrimePalindrome​(int n)
    Find the smallest prime palindrome greater than or equal to the input number.
    static boolean NormalizedPositive​(double[] array)
    Check if the Array Elements are Normalized and Positive
    static int NPK​(int n, int k)
    This function implements N Permute K.
    static int NthDigit​(int n)
    Identify the nth Digit of the Number
    static long NthUglyNumber​(long n, long a, long b, long c)
    Find the nth ugly number.
    static double PochhammerSymbol​(double s, int k)
    Compute the Pochhammer Symbol for the Specified s and k
    static double Power​(double x, int n)
    Compute the Integer Power of x
    static double PrimeFactorExponentTwo​(int n)
    Compute the Exponent 2 of Prime Factorization for a given Integer
    static boolean Print1DArray​(java.lang.String strName, double[] adblA, boolean bBailOnNaN)
    Print the contents of the 1D array
    static boolean Print1DArray​(java.lang.String strName, double[] adblA, int iNumDecimal, boolean bBailOnNaN)
    Print the contents of the 1D array to the Specified Decimal Location
    static boolean Print2DArray​(java.lang.String strName, double[][] aadblA, boolean bBailOnNaN)
    Print the contents of the 2D array
    static boolean Print2DArrayPair​(java.lang.String strLeftLabel, java.lang.String strRightLabel, double[][] aadblLeft, double[][] aadblRight, boolean bBailOnNaN)
    Print the Contents of the 2D Array Pair
    static boolean Print2DArrayTriplet​(java.lang.String strLeftLabel, java.lang.String strMiddleLabel, java.lang.String strRightLabel, double[][] aadblLeft, double[][] aadblMiddle, double[][] aadblRight, boolean bBailOnNaN)
    Print the Contents of the 2D Array Triplet
    static boolean PrintMatrix​(java.lang.String strName, double[][] aadblA)
    Print the Matrix Contents
    static int Quotient2​(int dividend, int divisor)
    Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
    static int ReciprocalIntegerFloor​(double z)
    Retrieve the Reciprocal Integer Floor of z
    static int ReverseInteger​(int number)
    Given a signed 32-bit integer number, return number with its digits reversed.
    static double RisingPochhammerSymbol​(double s, int k)
    Compute the Rising Pochhammer Symbol for the Specified s and k
    static boolean SameSign​(double[] adbl)
    Check if the specified array contains elements all of the same sign
    static double Sign​(double z)
    Indicate the Sign of z
    static double SubFactorial​(int n)
    This function implements Sub-factorial N.
    static int TrailingFactorialZeros​(int n)
    Estimate the Trailing Factorial Zeros
    static boolean WithinTolerance​(double dbl1, double dbl2)
    Compare and checks if the two input numbers fall within a specified tolerance
    static boolean WithinTolerance​(double dbl1, double dbl2, double dblAbsoluteTolerance, double dblRelativeTolerance)
    Compare and checks if the two input numbers fall within a specified tolerance

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NumberUtil

      public NumberUtil()
  • Method Details

    • IsValid

      public static final boolean IsValid​(long l)
      Check if the Input Long is MIN_VALUE or MAX_VALUE
      Parameters:
      l - Input Long
      Returns:
      TRUE - Input Long is MIN_VALUE or MAX_VALUE
    • IsValid

      public static final boolean IsValid​(long[] al)
      Check if the Input Long Array contains a MIN_VALUE or MAX_VALUE
      Parameters:
      al - Input Long Array
      Returns:
      TRUE - Input Long Array contains a MIN_VALUE or MAX_VALUE
    • IsValid

      public static final boolean IsValid​(double dbl)
      Checks if the input double is Infinite or NaN
      Parameters:
      dbl - Input double
      Returns:
      TRUE - Input double is Infinite or NaN
    • IsValid

      public static final boolean IsValid​(double[] adbl)
      Checks if the input double array contains an Infinite or an NaN
      Parameters:
      adbl - Input double array
      Returns:
      TRUE - Input double contains an Infinite or an NaN
    • WithinTolerance

      public static final boolean WithinTolerance​(double dbl1, double dbl2, double dblAbsoluteTolerance, double dblRelativeTolerance)
      Compare and checks if the two input numbers fall within a specified tolerance
      Parameters:
      dbl1 - Number #1
      dbl2 - Number #2
      dblAbsoluteTolerance - Absolute Tolerance
      dblRelativeTolerance - Relative Tolerance
      Returns:
      TRUE if they fall within the tolerance
    • WithinTolerance

      public static final boolean WithinTolerance​(double dbl1, double dbl2)
      Compare and checks if the two input numbers fall within a specified tolerance
      Parameters:
      dbl1 - Number #1
      dbl2 - Number #2
      Returns:
      TRUE if they fall within the tolerance
    • Factorial

      public static final int Factorial​(int n)
      This function implements Factorial N.
      Parameters:
      n - N
      Returns:
      Factorial N
    • SubFactorial

      public static final double SubFactorial​(int n)
      This function implements Sub-factorial N.
      Parameters:
      n - N
      Returns:
      The Sub-factorial N
    • NPK

      public static final int NPK​(int n, int k)
      This function implements N Permute K.
      Parameters:
      n - N
      k - K
      Returns:
      N Permute K
    • NCK

      public static final int NCK​(int n, int k)
      This function implements N choose K.
      Parameters:
      n - N
      k - K
      Returns:
      N choose K
    • Bound

      public static final double Bound​(double dblX, double dblFloor, double dblCeiling) throws java.lang.Exception
      Bound the input to within (floor, Ceiling), i.e., compute Min (Max (floor, X), Ceiling)
      Parameters:
      dblX - Input Number
      dblFloor - Floor
      dblCeiling - Ceiling
      Returns:
      Min (Max (floor, X), Ceiling)
      Throws:
      java.lang.Exception - Thrown if the inputs are invalid
    • Minimum

      public static final double Minimum​(double[] adbl) throws java.lang.Exception
      Retrieve the Minimum Element in the specified Array
      Parameters:
      adbl - Array of elements
      Returns:
      The Minimum Element
      Throws:
      java.lang.Exception - Thrown if the Inputs are invalid
    • Maximum

      public static final double Maximum​(double[] adbl) throws java.lang.Exception
      Retrieve the Maximum Element in the specified Array
      Parameters:
      adbl - Array of elements
      Returns:
      The Maximum Element
      Throws:
      java.lang.Exception - Thrown if the Inputs are invalid
    • SameSign

      public static final boolean SameSign​(double[] adbl) throws java.lang.Exception
      Check if the specified array contains elements all of the same sign
      Parameters:
      adbl - Array of elements
      Returns:
      TRUE - Same Sign
      Throws:
      java.lang.Exception - Thrown if the Inputs are invalid
    • Print1DArray

      public static final boolean Print1DArray​(java.lang.String strName, double[] adblA, boolean bBailOnNaN)
      Print the contents of the 1D array
      Parameters:
      strName - Label Name
      adblA - The 1D array
      bBailOnNaN - Bail on encountering an NaN
      Returns:
      TRUE - Print Successful
    • Print1DArray

      public static final boolean Print1DArray​(java.lang.String strName, double[] adblA, int iNumDecimal, boolean bBailOnNaN)
      Print the contents of the 1D array to the Specified Decimal Location
      Parameters:
      strName - Label Name
      adblA - The 1D array
      iNumDecimal - Number of Decimal Places to Display
      bBailOnNaN - Bail on encountering an NaN
      Returns:
      TRUE - Print Successful
    • Print2DArray

      public static final boolean Print2DArray​(java.lang.String strName, double[][] aadblA, boolean bBailOnNaN)
      Print the contents of the 2D array
      Parameters:
      strName - Label Name
      aadblA - The 2D array
      bBailOnNaN - Bail on encountering an NaN
      Returns:
      TRUE - Print Successful
    • Print2DArrayPair

      public static final boolean Print2DArrayPair​(java.lang.String strLeftLabel, java.lang.String strRightLabel, double[][] aadblLeft, double[][] aadblRight, boolean bBailOnNaN)
      Print the Contents of the 2D Array Pair
      Parameters:
      strLeftLabel - Left Label
      strRightLabel - Right Label
      aadblLeft - The Left 2D array
      aadblRight - The Right 2D array
      bBailOnNaN - Bail on encountering an NaN
      Returns:
      TRUE - Print Successful
    • Print2DArrayTriplet

      public static final boolean Print2DArrayTriplet​(java.lang.String strLeftLabel, java.lang.String strMiddleLabel, java.lang.String strRightLabel, double[][] aadblLeft, double[][] aadblMiddle, double[][] aadblRight, boolean bBailOnNaN)
      Print the Contents of the 2D Array Triplet
      Parameters:
      strLeftLabel - Left Label
      strMiddleLabel - Middle Label
      strRightLabel - Right Label
      aadblLeft - The Left 2D array
      aadblMiddle - The Middle 2D array
      aadblRight - The Right 2D array
      bBailOnNaN - Bail on encountering an NaN
      Returns:
      TRUE - Print Successful
    • PrintMatrix

      public static final boolean PrintMatrix​(java.lang.String strName, double[][] aadblA)
      Print the Matrix Contents
      Parameters:
      strName - Name of the Matrix
      aadblA - Matrix
      Returns:
      TRUE - Matrix Contents Successfully printed
    • HalfDownShiftedFactorial

      public static final double HalfDownShiftedFactorial​(int n) throws java.lang.Exception
      Compute (n - 0.5)!
      Parameters:
      n - n
      Returns:
      (n - 0.5)! Value
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • DoubleFactorial

      public static final double DoubleFactorial​(int n) throws java.lang.Exception
      Compute (2n - 1)!!
      Parameters:
      n - n
      Returns:
      (2n - 1)!!
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • RisingPochhammerSymbol

      public static final double RisingPochhammerSymbol​(double s, int k) throws java.lang.Exception
      Compute the Rising Pochhammer Symbol for the Specified s and k
      Parameters:
      s - s
      k - k
      Returns:
      The Rising Pochhammer Symbol
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • PochhammerSymbol

      public static final double PochhammerSymbol​(double s, int k) throws java.lang.Exception
      Compute the Pochhammer Symbol for the Specified s and k
      Parameters:
      s - s
      k - k
      Returns:
      Pochhammer Symbol
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • IsInteger

      public static final boolean IsInteger​(double z) throws java.lang.Exception
      Indicate if z is an Integer
      Parameters:
      z - Z
      Returns:
      TRUE - z is an Integer
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • IsPositiveInteger

      public static final boolean IsPositiveInteger​(double z) throws java.lang.Exception
      Indicate if z is a Positive Integer
      Parameters:
      z - Z
      Returns:
      TRUE - z is a Positive Integer
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • IsNonPositiveInteger

      public static final boolean IsNonPositiveInteger​(double z) throws java.lang.Exception
      Indicate if z is a Non-Positive Integer
      Parameters:
      z - Z
      Returns:
      TRUE - z is a Non-Positive Integer
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • IsNegativeInteger

      public static final boolean IsNegativeInteger​(double z) throws java.lang.Exception
      Indicate if z is a Negative Integer
      Parameters:
      z - Z
      Returns:
      TRUE - z is a Negative Integer
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • IsNonNegativeInteger

      public static final boolean IsNonNegativeInteger​(double z) throws java.lang.Exception
      Indicate if z is a Non-Negative Integer
      Parameters:
      z - Z
      Returns:
      TRUE - z is a Non-Negative Integer
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • Sign

      public static final double Sign​(double z) throws java.lang.Exception
      Indicate the Sign of z
      Parameters:
      z - Z
      Returns:
      Sign of z
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • NormalizedPositive

      public static final boolean NormalizedPositive​(double[] array)
      Check if the Array Elements are Normalized and Positive
      Parameters:
      array - Array
      Returns:
      TRUE - The Array Elements are Normalized and Positive
    • Fractional

      public static final double Fractional​(double z) throws java.lang.Exception
      Retrieve the Fractional Part of z
      Parameters:
      z - Z
      Returns:
      The Fractional Part of z
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • ReciprocalIntegerFloor

      public static final int ReciprocalIntegerFloor​(double z) throws java.lang.Exception
      Retrieve the Reciprocal Integer Floor of z
      Parameters:
      z - Z
      Returns:
      The Reciprocal Integer Floor of z
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • PrimeFactorExponentTwo

      public static final double PrimeFactorExponentTwo​(int n) throws java.lang.Exception
      Compute the Exponent 2 of Prime Factorization for a given Integer
      Parameters:
      n - The Integer
      Returns:
      Exponent 2 of Prime Factorization for a given Integer
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • BinaryDigitCount

      public static final int BinaryDigitCount​(int n)
      Retrieve the Binary Digit Count
      Parameters:
      n - N
      Returns:
      The Binary Digit Count
    • DivideIntegers

      public static final int DivideIntegers​(int dividend, int divisor) throws java.lang.Exception
      Divide two integers without using multiplication, division, and mod operator. The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2.
      Parameters:
      dividend - Dividend
      divisor - Divisor
      Returns:
      The quotient after dividing dividend by divisor
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • Power

      public static final double Power​(double x, int n) throws java.lang.Exception
      Compute the Integer Power of x
      Parameters:
      x - x
      n - n
      Returns:
      Integer Power of x
      Throws:
      java.lang.Exception - Thrown if the Inputs are Invalid
    • NextPrimePalindrome

      public static final int NextPrimePalindrome​(int n)
      Find the smallest prime palindrome greater than or equal to the input number. Recall that a number is prime if it's only divisors are 1 and itself, and it is greater than 1. For example, 2,3,5,7,11 and 13 are primes. Recall that a number is a palindrome if it reads the same from left to right as it does from right to left. For example, 12321 is a palindrome.
      Parameters:
      n - The Number
      Returns:
      The Next Prime Palindrome
    • GCD

      public static final long GCD​(long a, long b)
      Greatest Common Divisor between Two Numbers
      Parameters:
      a - First Number
      b - Second Number
      Returns:
      The GCD
    • NthUglyNumber

      public static final long NthUglyNumber​(long n, long a, long b, long c)
      Find the nth ugly number. Ugly numbers are positive integers which are divisible by a or b or c.
      Parameters:
      n - n
      a - A
      b - B
      c - C
      Returns:
      The nth Ugly Number
    • IsNumberSequenceAdditive

      public static final boolean IsNumberSequenceAdditive​(java.lang.String s)
      Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the sequence must be the sum of the preceding two. Given a string containing only digits '0'-'9', write a function to determine if it's an additive number. Note: Numbers in the additive sequence cannot have leading zeros, so sequence 1, 2, 03 or 1, 02, 3 is invalid.
      Parameters:
      s - The Input String
      Returns:
      TRUE - Input is an Additive Number
    • TrailingFactorialZeros

      public static final int TrailingFactorialZeros​(int n)
      Estimate the Trailing Factorial Zeros
      Parameters:
      n - Factorial NUmber
      Returns:
      Number of Training Factor Zeros
    • CanMeasureWater

      public static final boolean CanMeasureWater​(int x, int y, int z)
      You are given two jugs with capacities x and y liters. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z liters using these two jugs. If z liters of water is measurable, you must have z liters of water contained within one or both buckets by the end. Operations allowed: Fill any of the jugs completely with water. Empty any of the jugs. Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.
      Parameters:
      x - x
      y - y
      z - z
      Returns:
      TRUE - The Measurement can be successfully carried out
    • NthDigit

      public static final int NthDigit​(int n)
      Identify the nth Digit of the Number
      Parameters:
      n - The Number
      Returns:
      The nth Digit
    • Quotient2

      public static final int Quotient2​(int dividend, int divisor)
      Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator. Return the quotient after dividing dividend by divisor. The integer division should truncate toward zero, which means losing its fractional part. For example, truncate(8.345) = 8 and truncate(-2.7335) = -2. Note: Assume we are dealing with an environment that could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For this problem, assume that your function returns 231 − 1 when the division result overflows.
      Parameters:
      dividend - Dividend
      divisor - Divisor
      Returns:
      Quotient
    • ReverseInteger

      public static final int ReverseInteger​(int number)
      Given a signed 32-bit integer number, return number with its digits reversed. If reversing number causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
      Parameters:
      number - The Given Number
      Returns:
      The Reverse